AS AgentScopeAPI 手册 / API Reference

AgentStateStore

public interface AgentStateStore
源码Source 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. null represents 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

方法Method说明Description
public void save(String userId, String sessionId, String key, State value)
保存数据到持久化存储。
Save a single state value (full replacement).
default public boolean supportsVersioning()
default
返回 `supportsVersioning` 结果。
Whether this backend supports optimistic concurrency via `#getVersioned` / `#saveIfVersion`.
default public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
default
获取 `getVersioned`。
Load a single state value together with its store version.
default public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
default
保存If 版本。
Compare-and-swap write of a single state value.
public void save(String userId, String sessionId, String key, List<? extends State> values)
状态值的保存列出。
Save a list of state values.
public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)
按条件读取数据。
Get a single state value.
public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
获取列出的状态值。
Get a list of state values.
public boolean exists(String userId, String sessionId)
判断数据是否存在。
Check if a session exists.
public void delete(String userId, String sessionId)
删除数据。
Delete a session and all its data.
default public void delete(String userId, String sessionId, String key)
default
删除数据。
Delete a single state entry within a session.
public Set<String> listSessionIds(String userId)
列出 `listSessionIds`。
List session identifiers visible under the given user namespace.
default public void close()
default
关闭连接并释放相关资源。
Clean up any resources used by this store.

字段详情Field Detail

UNVERSIONED

long UNVERSIONED

UNVERSIONED字段。声明为 long UNVERSIONED。

Sentinel version for backends that do not support optimistic concurrency.

源码Source 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).

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier; `null` = anonymous
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString键(类型 `String`)。the state key (e.g., `"agent_state"`, `"toolkit_activeGroups"`)
valueState状态值到保存。the state value to save
源码Source io/agentscope/core/state/AgentStateStore.java:79

supportsVersioning

default public boolean supportsVersioning()
default

返回 supportsVersioning 结果。

Whether this backend supports optimistic concurrency via #getVersioned / #saveIfVersion.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/core/state/AgentStateStore.java:87–89

getVersioned

default public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
default

获取 getVersioned。

Load a single state value together with its store version.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString状态密钥。the state key
typeClass<T>类型(类型 `Class`)。the expected state type
返回Returns VersionedState<T>
返回VersionedState。a `VersionedState`; `value` is `null` when absent. On versioning backends an absent key reports `version == 0`.
源码Source io/agentscope/core/state/AgentStateStore.java:104–107

saveIfVersion

default public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
default

保存If 版本。

Compare-and-swap write of a single state value.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString状态密钥。the state key
valueState新的状态值。the new state value
expectedVersionlongexpected 版本(类型 `long`)。the version observed by the caller
返回Returns long
返回数值结果。the new version on success, or `#UNVERSIONED` on conflict / non-versioning backends
源码Source io/agentscope/core/state/AgentStateStore.java:134–138

save

public void save(String userId, String sessionId, String key, List<? extends State> values)

状态值的保存列出。

Save a list of state values.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString键(类型 `String`)。the state key (e.g., `"memory_messages"`)
valuesList<? extends State>值(类型 `List`)。the full list of state values
源码Source io/agentscope/core/state/AgentStateStore.java:157

get

public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)

按条件读取数据。

Get a single state value.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString状态密钥。the state key
typeClass<T>类型(类型 `Class`)。the expected state type
返回Returns Optional<T>
返回可选。the state value, or empty if not found
源码Source io/agentscope/core/state/AgentStateStore.java:169

getList

public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)

获取列出的状态值。

Get a list of state values.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString状态密钥。the state key
itemTypeClass<T>条目类型(类型 `Class`)。the expected item type
返回Returns List<T>
返回列出。the list of state values, or empty list if not found
源码Source io/agentscope/core/state/AgentStateStore.java:181–182

exists

public boolean exists(String userId, String sessionId)

判断数据是否存在。

Check if a session exists.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
返回Returns boolean
返回布尔值,表示判断结果。true if the session has any persisted state
源码Source io/agentscope/core/state/AgentStateStore.java:191

delete

public void delete(String userId, String sessionId)

删除数据。

Delete a session and all its data.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
源码Source io/agentscope/core/state/AgentStateStore.java:199

delete

default public void delete(String userId, String sessionId, String key)
default

删除数据。

Delete a single state entry within a session.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
sessionIdString会话标识(类型 `String`)。session identifier; must be non-null and non-blank
keyString状态密钥到删除。the state key to delete
源码Source io/agentscope/core/state/AgentStateStore.java:208–210

listSessionIds

public Set<String> listSessionIds(String userId)

列出 listSessionIds。

List session identifiers visible under the given user namespace.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。nullable user identifier
返回Returns Set<String>
返回设置。set of session identifiers stored under `userId`
源码Source io/agentscope/core/state/AgentStateStore.java:222

close

default public void close()
default

关闭连接并释放相关资源。

Clean up any resources used by this store.

源码Source io/agentscope/core/state/AgentStateStore.java:228–230
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.