agentscope.session¶
The session module in agentscope.
- class SessionBase[源代码]¶
基类:
objectThe base class for session in agentscope.
- abstract async save_session_state(session_id, user_id='', **state_modules_mapping)[源代码]¶
Save the session state
- 参数:
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.
- 返回类型:
None
- abstract async load_session_state(session_id, user_id='', allow_not_exist=True, **state_modules_mapping)[源代码]¶
Load the session state
- 参数:
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.
- 返回类型:
None
- class JSONSession[源代码]¶
基类:
SessionBaseThe JSON session class.
- __init__(save_dir='./')[源代码]¶
Initialize the JSON session class.
- 参数:
save_dir (str, defaults to `"./") -- The directory to save the session state.
- 返回类型:
None
- async save_session_state(session_id, user_id='', **state_modules_mapping)[源代码]¶
Load the state dictionary from a JSON file.
- 参数:
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.
- 返回类型:
None
- async load_session_state(session_id, user_id='', allow_not_exist=True, **state_modules_mapping)[源代码]¶
Get the state dictionary to be saved to a JSON file.
- 参数:
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.
- 返回类型:
None
- class RedisSession[源代码]¶
基类:
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)[源代码]¶
Initialize the Redis session class by connecting to Redis.
- 参数:
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.
- 返回类型:
None
- async save_session_state(session_id, user_id='default_user', **state_modules_mapping)[源代码]¶
Save the state dictionary to Redis.
- 参数:
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.
- 返回类型:
None
- async load_session_state(session_id, user_id='default_user', allow_not_exist=True, **state_modules_mapping)[源代码]¶
Load the state dictionary from Redis.
- 参数:
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.
- 返回类型:
None