agentscope.session¶
The session module in agentscope.
- class SessionBase[source]¶
Bases:
objectThe base class for session in agentscope.
- abstract async save_session_state(session_id, user_id='', **state_modules_mapping)[source]¶
Save the session state
- Parameters:
session_id (str) – The session id.
user_id (str, default to “”) – The user ID for the storage.
**state_modules_mapping (dict[str, StateModule]) – A dictionary mapping of state module names to their instances.
- Return type:
None
- abstract async load_session_state(session_id, user_id='', allow_not_exist=True, **state_modules_mapping)[source]¶
Load the session state
- Parameters:
session_id (str) – The session id.
user_id (str, default to “”) – The user ID for the storage.
allow_not_exist (bool, defaults to True) – Whether to allow the session to not exist.
**state_modules_mapping (dict[str, StateModule]) – The mapping of state modules to be loaded.
- Return type:
None
- class JSONSession[source]¶
Bases:
SessionBaseThe JSON session class.
- __init__(save_dir='./')[source]¶
Initialize the JSON session class.
- Parameters:
save_dir (str, defaults to `”./”) – The directory to save the session state.
- Return type:
None
- async save_session_state(session_id, user_id='', **state_modules_mapping)[source]¶
Load the state dictionary from a JSON file.
- Parameters:
session_id (str) – The session id.
user_id (str, default to “”) – The user ID for the storage.
**state_modules_mapping (dict[str, StateModule]) – A dictionary mapping of state module names to their instances.
- Return type:
None
- async load_session_state(session_id, user_id='', allow_not_exist=True, **state_modules_mapping)[source]¶
Get the state dictionary to be saved to a JSON file.
- Parameters:
session_id (str) – The session id.
user_id (str, default to “”) – The user ID for the storage.
allow_not_exist (bool, defaults to True) – Whether to allow the session to not exist. If False, raises an error if the session does not exist.
state_modules_mapping (list[StateModule]) – The list of state modules to be loaded.
- Return type:
None
- class RedisSession[source]¶
Bases:
SessionBaseThe Redis session class.
- SESSION_KEY = 'user_id:{user_id}:session:{session_id}:state'¶
Redis key pattern (without prefix) for storing sessions state for a specific session.
- __init__(host='localhost', port=6379, db=0, password=None, connection_pool=None, key_ttl=None, key_prefix='', **kwargs)[source]¶
Initialize the Redis session class by connecting to Redis.
- Parameters:
host (str, defaults to “localhost”) – Redis server host.
port (int, defaults to 6379) – Redis server port.
db (int, defaults to 0) – Redis database index.
password (str | None, optional) – Redis password if required.
connection_pool (ConnectionPool | None, optional) – Optional Redis connection pool.
key_ttl (int | None, optional) – Expire time in seconds for each session state key. If provided, the expiration will be refreshed on every save/load (sliding TTL). If None, the session state will not expire.
key_prefix (str, default to “”) – Optional Redis key prefix prepended to every key generated by this storage. Useful for isolating keys across apps/environments (e.g. “prod:”, “staging:”, “myapp:”).
**kwargs (Any) – Additional keyword arguments passed to redis client.
- Return type:
None
- get_client()[source]¶
Get the underlying Redis client.
- Returns:
The Redis client instance.
- Return type:
Redis
- async save_session_state(session_id, user_id='default_user', **state_modules_mapping)[source]¶
Save the state dictionary to Redis.
- Parameters:
session_id (str) – The session id.
user_id (str, default to “default_user”) – The user ID for the storage.
**state_modules_mapping (dict[str, StateModule]) – A dictionary mapping of state module names to their instances.
- Return type:
None
- async load_session_state(session_id, user_id='default_user', allow_not_exist=True, **state_modules_mapping)[source]¶
Load the state dictionary from Redis.
- Parameters:
session_id (str) – The session id.
user_id (str, default to “default_user”) – The user ID for the storage.
allow_not_exist (bool, defaults to True) – Whether to allow the session to not exist.
**state_modules_mapping (dict[str, StateModule]) – The mapping of state modules to be loaded.
- Return type:
None