接口 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.
save(SessionKey, String, State)- Save a single state objectsave(SessionKey, String, List)- Save a list (incremental append)get(SessionKey, String, Class)- Get a single state objectgetList(SessionKey, String, Class)- Get a list of state objects
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);
-
方法概要
修饰符和类型方法说明default voidclose()Clean up any resources used by this session manager.voiddelete(SessionKey sessionKey) Delete a session and all its data.default voiddelete(SessionKey sessionKey, String key) Delete a single state entry within a session.booleanexists(SessionKey sessionKey) Check if a session exists.get(SessionKey sessionKey, String key, Class<T> type) Get a single state value.getList(SessionKey sessionKey, String key, Class<T> itemType) Get a list of state values.List all session keys.voidsave(SessionKey sessionKey, String key, State value) Save a single state value (full replacement).voidsave(SessionKey sessionKey, String key, List<? extends State> values) Save a list of state values.
-
方法详细资料
-
save
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 identifierkey- the state key (e.g., "agent_meta", "toolkit_activeGroups")value- the state value to save
-
save
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 identifierkey- the state key (e.g., "memory_messages")values- the full list of state values
-
get
Get a single state value.- 类型参数:
T- the state type- 参数:
sessionKey- the session identifierkey- the state keytype- the expected state type- 返回:
- the state value, or empty if not found
-
getList
Get a list of state values.- 类型参数:
T- the item type- 参数:
sessionKey- the session identifierkey- the state keyitemType- the expected item type- 返回:
- the list of state values, or empty list if not found
-
exists
Check if a session exists.- 参数:
sessionKey- the session identifier- 返回:
- true if the session exists
-
delete
Delete a session and all its data.- 参数:
sessionKey- the session identifier
-
delete
Delete a single state entry within a session.- 参数:
sessionKey- the session identifierkey- 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.
-