JsonFileAgentStateStore
public class JsonFileAgentStateStore implements AgentStateStore
io/agentscope/core/state/JsonFileAgentStateStore.java:69–417把 AgentState 按用户与会话写入本地 JSON 文件,适合单机开发与调试。位于 io.agentscope.core.state 包,实现 AgentStateStore。核心成员包括 save、get、getList、exists、delete、listSessionIds。
JSON file-based AgentStateStore implementation.
On-disk layout:
<root>/
__anon__/ ← anonymous (userId == null) sessions
<safe(sessionId)>/
agent_state.json
memory_messages.jsonl
memory_messages.hash
<safe(userId)>/ ← per-user sessions
<safe(sessionId)>/
agent_state.json
...
safe(...) keeps the literal string when it matches
^[a-zA-Z0-9_\-.]+$; otherwise it is Base64-URL encoded (no padding) so the
filesystem accepts it.
Features: atomic file operations, UTF-8 encoding, graceful handling of missing sessions, hash-based append-or-rewrite for list state.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public JsonFileAgentStateStore() | 在指定目录创建 JSON 文件状态存储。 Create a `JsonFileAgentStateStore` with the default root:
`~/.agentscope/state`. |
public JsonFileAgentStateStore(Path rootDirectory) | 在指定目录创建 JSON 文件状态存储。 Create a `JsonFileAgentStateStore` rooted at the given directory. |
方法摘要Method Summary
构造器详情Constructor Detail
JsonFileAgentStateStore
public JsonFileAgentStateStore()
创建JsonFileAgentStateStore使用默认根:~/.agentscope/state。
Create a JsonFileAgentStateStore with the default root: ~/.agentscope/state.
io/agentscope/core/state/JsonFileAgentStateStore.java:83–85JsonFileAgentStateStore
public JsonFileAgentStateStore(Path rootDirectory)
初始化 JsonFileAgentStateStore 实例。
Create a JsonFileAgentStateStore rooted at the given directory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rootDirectory | Path | 根目录(类型 `Path`)。root directory under which user / session sub-directories live |
io/agentscope/core/state/JsonFileAgentStateStore.java:92–99方法详情Method Detail
save
public void save(String userId, String sessionId, String key, State value)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
value | State | 值(类型 `State`)。The value. |
io/agentscope/core/state/JsonFileAgentStateStore.java:101–111save
public void save(String userId, String sessionId, String key, List<? extends State> values)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
values | List<? extends State> | 值(类型 `List`)。The values. |
io/agentscope/core/state/JsonFileAgentStateStore.java:113–136get
public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)
按条件读取数据。
Performs the get operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
type | Class<T> | 类型(类型 `Class`)。The type. |
Optional<T>io/agentscope/core/state/JsonFileAgentStateStore.java:165–178getList
public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
获取列表。
Returns the list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
itemType | Class<T> | 条目类型(类型 `Class`)。The item type. |
List<T>io/agentscope/core/state/JsonFileAgentStateStore.java:180–201exists
public boolean exists(String userId, String sessionId)
判断数据是否存在。
Performs the exists operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
booleanio/agentscope/core/state/JsonFileAgentStateStore.java:203–206delete
public void delete(String userId, String sessionId)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
io/agentscope/core/state/JsonFileAgentStateStore.java:208–214delete
public void delete(String userId, String sessionId, String key)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
key | String | 键(类型 `String`)。The key. |
io/agentscope/core/state/JsonFileAgentStateStore.java:216–223listSessionIds
public Set<String> listSessionIds(String userId)
列出 listSessionIds。
Lists the session ids.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
Set<String>io/agentscope/core/state/JsonFileAgentStateStore.java:225–240getRootDirectory
public Path getRootDirectory()
获取根目录。
Returns the configured root directory (for diagnostics).
Pathio/agentscope/core/state/JsonFileAgentStateStore.java:243–245clearAllSessions
public Mono<Integer> clearAllSessions()
清空所有会话。
Clear every session under every user.
Mono<Integer>io/agentscope/core/state/JsonFileAgentStateStore.java:250–279getSessionDir
protected Path getSessionDir(String userId, String sessionId)
获取会话目录。
Returns the directory under which all state files for (userId, sessionId) live.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
sessionId | String | 会话标识(类型 `String`)。The session id. |
Pathio/agentscope/core/state/JsonFileAgentStateStore.java:285–289