MongoAgentStateStore
public class MongoAgentStateStore implements AgentStateStore, AutoCloseable
io/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:100–730Mongo 智能体状态存储,负责持久化与读取数据。位于 io.agentscope.extensions.mongodb.state 包,实现 AgentStateStore、AutoCloseable。核心成员包括 builder、supportsVersioning、save、get、getList、getVersioned。
MongoDB-backed implementation of AgentStateStore.
Each session is stored as a single MongoDB document with a compound _id of
{user, session} to prevent cross-tenant key collisions. State keys are stored inside
a states sub-document (via dot notation, e.g. states.foo), keeping them
isolated from reserved top-level fields such as _id, user_id,
session_id, and _updated_at. Per-key versions live in a versions
sub-document and list-content hashes in a hashes sub-document.
Supports optimistic concurrency via the versions.<key> field. List state uses
ListHashUtil for change detection to avoid unnecessary full rewrites. The list
element count is obtained server-side via $size aggregation to avoid transferring
the full array just to call .size().
Usage:
`MongoAgentStateStore store = MongoAgentStateStore.builder()
.connectionString("mongodb://localhost:27017")
.databaseName("agentscope")
.collectionName("sessions")
.build();
`
Key constraints
-
State key character set: keys passed to
#save/#getmust match^[a-zA-Z_][a-zA-Z0-9_]*$— no dots, dollar signs, or Unicode. Values insideMap<String, Object>fields are automatically escaped byMongoKeyEscaperand have no character restrictions. -
Snapshots use GridFS: sandbox workspace snapshots are stored in GridFS (up to 16 GB per file), not as single BSON documents. The legacy 16 MB BSON limit no longer applies.
-
Session TTL is off by default: sessions are retained indefinitely, aligned with Postgres/JDBC/Redis. To enable automatic expiry, pass
MongoAgentStateStore.builder().ttlDays(n)with a positive integer. The TTL index is sparse — only documents with a non-null_updated_atfield expire.
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | `MongoAgentStateStore`的构建器。 Builder for `MongoAgentStateStore`. |
方法摘要Method Summary
方法详情Method Detail
builder
public static Builder builder()
返回构建器实例。
Returns the builder.
Builderio/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:155–157supportsVersioning
public boolean supportsVersioning()
返回 supportsVersioning 结果。
Returns the supports versioning.
booleanio/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:159–162save
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/extensions/mongodb/state/MongoAgentStateStore.java:185–202get
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/extensions/mongodb/state/MongoAgentStateStore.java:204–223save
public void save(String userId, String sessionId, String key, List<? extends State> values)
保存数据到持久化存储。
Saves a list of state values with incremental-append optimization.
| 名称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/extensions/mongodb/state/MongoAgentStateStore.java:242–327getList
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/extensions/mongodb/state/MongoAgentStateStore.java:329–356getVersioned
public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
获取 getVersioned。
Returns the versioned.
| 名称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. |
VersionedState<T>io/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:360–383saveIfVersion
public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
保存If 版本。
Saves the if version.
| 名称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. |
expectedVersion | long | expected 版本(类型 `long`)。The expected version. |
longio/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:385–494exists
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/extensions/mongodb/state/MongoAgentStateStore.java:498–503delete
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/extensions/mongodb/state/MongoAgentStateStore.java:505–520delete
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/extensions/mongodb/state/MongoAgentStateStore.java:522–533listSessionIds
public Set<String> listSessionIds(String userId)
列出 listSessionIds。
Lists the session ids.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。The user id. |
Set<String>io/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:535–546close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/extensions/mongodb/state/MongoAgentStateStore.java:550–555嵌套类详情Nested Class Detail
Builder
public static class BuilderMongoAgentStateStore的构建器。位于 io.agentscope.extensions.mongodb.state 包。核心成员包括 mongoClient、connectionString、databaseName、collectionName、ttlDays、onDeleteCallback。
Builder for MongoAgentStateStore.