接口 AgentStateStore
An AgentStateStore provides save / load / delete / list operations for
State objects keyed by a (userId, sessionId) pair, allowing agents,
memories, toolkits, and other stateful components to be persisted and restored across
application runs or user interactions.
Slot addressing is intentionally simple:
sessionId— non-null, non-blank; identifies a conversation / session.userId— nullable.nullrepresents an anonymous / single-tenant caller (CLI usage, tests). Implementations group all anonymous sessions under a single namespace.
Implementations decide how to combine the pair into a storage key (filesystem path, Redis key prefix, SQL column). Callers MUST NOT concatenate them manually before calling.
Example usage:
AgentStateStore store = new JsonFileAgentStateStore(Path.of("state"));
// Save state for an anonymous session
store.save(null, "session-1", "agent_state", state);
// Save state scoped to a user
store.save("alice", "session-1", "agent_state", state);
// Load state
Optional<AgentState> loaded =
store.get("alice", "session-1", "agent_state", AgentState.class);
// List all sessions owned by a user (null lists anonymous sessions)
Set<String> mySessions = store.listSessionIds("alice");
-
方法概要
修饰符和类型方法说明default voidclose()Clean up any resources used by this store.voidDelete a session and all its data.default voidDelete a single state entry within a session.booleanCheck if a session exists.Get a single state value.Get a list of state values.listSessionIds(String userId) List session identifiers visible under the given user namespace.voidSave a single state value (full replacement).voidSave 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.
- 参数:
userId- nullable user identifier;null= anonymoussessionId- session identifier; must be non-null and non-blankkey- the state key (e.g.,"agent_state","toolkit_activeGroups")value- the state value to save
-
save
Save a list of state values.Different implementations may use different storage strategies:
JsonFileAgentStateStore: incremental append — only new elements are writtenInMemoryAgentStateStore: full replacement — replaces the entire list
Callers should always pass the full list. The implementation decides the storage strategy.
- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blankkey- the state key (e.g.,"memory_messages")values- the full list of state values
-
get
Get a single state value.- 类型参数:
T- the state type- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blankkey- 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- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blankkey- the state keyitemType- the expected item type- 返回:
- the list of state values, or empty list if not found
-
exists
Check if a session exists.- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blank- 返回:
- true if the session has any persisted state
-
delete
Delete a session and all its data.- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blank
-
delete
Delete a single state entry within a session.- 参数:
userId- nullable user identifiersessionId- session identifier; must be non-null and non-blankkey- the state key to delete
-
listSessionIds
List session identifiers visible under the given user namespace.Use
userId == nullto list anonymous sessions. Pass a concrete user to list only that user's sessions. There is no API to list across users in one call — that is a separate administrative concern (admin starter handles it by iterating known users).- 参数:
userId- nullable user identifier- 返回:
- set of session identifiers stored under
userId
-
close
default void close()Clean up any resources used by this store. Implementations should override this if they need cleanup.
-