AgentStateStore
public interface AgentStateStore
io/agentscope/core/state/AgentStateStore.java:61–231定义智能体状态存储契约的接口。位于 io.agentscope.core.state 包。核心成员包括 save、supportsVersioning、getVersioned、saveIfVersion、get、getList。
Persistent storage interface for AgentScope agent state.
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");
`
字段摘要Field Summary
| 字段Field | 类型Type | 说明Description |
|---|---|---|
UNVERSIONED | long | UNVERSIONED字段。 Sentinel version for backends that do not support optimistic concurrency. |
方法摘要Method Summary
字段详情Field Detail
UNVERSIONED
long UNVERSIONED
UNVERSIONED字段。声明为 long UNVERSIONED。
Sentinel version for backends that do not support optimistic concurrency.
io/agentscope/core/state/AgentStateStore.java:67方法详情Method Detail
save
public void save(String userId, String sessionId, String key, State value)
保存数据到持久化存储。
Save a single state value (full replacement).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier; `null` = anonymous |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 键(类型 `String`)。the state key (e.g., `"agent_state"`, `"toolkit_activeGroups"`) |
value | State | 状态值到保存。the state value to save |
io/agentscope/core/state/AgentStateStore.java:79supportsVersioning
default public boolean supportsVersioning()
返回 supportsVersioning 结果。
Whether this backend supports optimistic concurrency via #getVersioned / #saveIfVersion.
booleanio/agentscope/core/state/AgentStateStore.java:87–89getVersioned
default public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
获取 getVersioned。
Load a single state value together with its store version.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 状态密钥。the state key |
type | Class<T> | 类型(类型 `Class`)。the expected state type |
VersionedState<T>io/agentscope/core/state/AgentStateStore.java:104–107saveIfVersion
default public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
保存If 版本。
Compare-and-swap write of a single state value.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 状态密钥。the state key |
value | State | 新的状态值。the new state value |
expectedVersion | long | expected 版本(类型 `long`)。the version observed by the caller |
longio/agentscope/core/state/AgentStateStore.java:134–138save
public void save(String userId, String sessionId, String key, List<? extends State> values)
状态值的保存列出。
Save a list of state values.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 键(类型 `String`)。the state key (e.g., `"memory_messages"`) |
values | List<? extends State> | 值(类型 `List`)。the full list of state values |
io/agentscope/core/state/AgentStateStore.java:157get
public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)
按条件读取数据。
Get a single state value.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 状态密钥。the state key |
type | Class<T> | 类型(类型 `Class`)。the expected state type |
Optional<T>io/agentscope/core/state/AgentStateStore.java:169getList
public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
获取列出的状态值。
Get a list of state values.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 状态密钥。the state key |
itemType | Class<T> | 条目类型(类型 `Class`)。the expected item type |
List<T>io/agentscope/core/state/AgentStateStore.java:181–182exists
public boolean exists(String userId, String sessionId)
判断数据是否存在。
Check if a session exists.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
booleanio/agentscope/core/state/AgentStateStore.java:191delete
public void delete(String userId, String sessionId)
删除数据。
Delete a session and all its data.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
io/agentscope/core/state/AgentStateStore.java:199delete
default public void delete(String userId, String sessionId, String key)
删除数据。
Delete a single state entry within a session.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
sessionId | String | 会话标识(类型 `String`)。session identifier; must be non-null and non-blank |
key | String | 状态密钥到删除。the state key to delete |
io/agentscope/core/state/AgentStateStore.java:208–210listSessionIds
public Set<String> listSessionIds(String userId)
列出 listSessionIds。
List session identifiers visible under the given user namespace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。nullable user identifier |
Set<String>io/agentscope/core/state/AgentStateStore.java:222close
default public void close()
关闭连接并释放相关资源。
Clean up any resources used by this store.
io/agentscope/core/state/AgentStateStore.java:228–230