MysqlAgentStateStore
public class MysqlAgentStateStore implements AgentStateStore
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:69–1043MySQL 智能体状态存储,负责持久化与读取数据。位于 io.agentscope.extensions.mysql.state 包,实现 AgentStateStore。核心成员包括 supportsVersioning、save、getVersioned、saveIfVersion、get、getList。
MySQL database-based session implementation.
This implementation stores session state in MySQL database tables with the following structure:
-
Single state: stored as JSON with item_index = 0
-
List state: each item stored in a separate row with item_index = 0, 1, 2, ...
Table Schema (auto-created if createIfNotExist=true):
CREATE TABLE IF NOT EXISTS agentscope_sessions (
session_id VARCHAR(255) NOT NULL,
state_key VARCHAR(255) NOT NULL,
item_index INT NOT NULL DEFAULT 0,
state_data LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (session_id, state_key, item_index)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Features:
-
True incremental list storage (only INSERTs new items, no read-modify-write)
-
Type-safe state serialization using Jackson
-
Automatic table creation
-
SQL injection prevention through parameterized queries
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public MysqlAgentStateStore(DataSource dataSource) | 创建MysqlAgentStateStore 使用默认设置。 Create a MysqlAgentStateStore with default settings. |
public MysqlAgentStateStore(DataSource dataSource, boolean createIfNotExist) | 初始化 `MysqlAgentStateStore` 实例。 Create a MysqlAgentStateStore with optional auto-creation of database and table. |
public MysqlAgentStateStore(DataSource dataSource, String databaseName, String tableName, boolean createIfNotExist) | 初始化 `MysqlAgentStateStore` 实例。 Create a MysqlAgentStateStore with custom database name, table name, and optional auto-creation. |
方法摘要Method Summary
构造器详情Constructor Detail
MysqlAgentStateStore
public MysqlAgentStateStore(DataSource dataSource)
创建MysqlAgentStateStore 使用默认设置。
Create a MysqlAgentStateStore with default settings.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if database or table does not exist
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:111–113MysqlAgentStateStore
public MysqlAgentStateStore(DataSource dataSource, boolean createIfNotExist)
初始化 MysqlAgentStateStore 实例。
Create a MysqlAgentStateStore with optional auto-creation of database and table.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:128–130MysqlAgentStateStore
public MysqlAgentStateStore(DataSource dataSource, String databaseName, String tableName, boolean createIfNotExist)
初始化 MysqlAgentStateStore 实例。
Create a MysqlAgentStateStore with custom database name, table name, and optional auto-creation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
databaseName | String | 数据库名称(类型 `String`)。Custom database name (uses default if null or empty) |
tableName | String | table 名称(类型 `String`)。Custom table name (uses default if null or empty) |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:146–179方法详情Method Detail
supportsVersioning
public boolean supportsVersioning()
返回 supportsVersioning 结果。
Returns the supports versioning.
booleanio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:357–360save
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/mysql/state/MysqlAgentStateStore.java:362–394getVersioned
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/mysql/state/MysqlAgentStateStore.java:396–427saveIfVersion
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/mysql/state/MysqlAgentStateStore.java:454–489save
public void save(String userId, String sessionId, String key, List<? extends State> values)
保存数据到持久化存储。
Save a list of state values with hash-based change detection.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
userId | String | 用户标识(类型 `String`)。the user identifier |
sessionId | String | 会话标识(类型 `String`)。the session identifier |
key | String | 键(类型 `String`)。the state key (e.g., "memory_messages") |
values | List<? extends State> | 状态值到保存的列出。the list of state values to save |
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:555–591get
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/mysql/state/MysqlAgentStateStore.java:744–774getList
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/mysql/state/MysqlAgentStateStore.java:776–807exists
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/mysql/state/MysqlAgentStateStore.java:809–827delete
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/mysql/state/MysqlAgentStateStore.java:829–848listSessionIds
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/mysql/state/MysqlAgentStateStore.java:850–875close
public void close()
关闭连接并释放相关资源。
Close the session and release any resources.
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:901–904getDatabaseName
public String getDatabaseName()
获取数据库名称。
Get the database name used for storing sessions.
Stringio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:911–913getTableName
public String getTableName()
获取 getTableName。
Get the table name used for storing sessions.
Stringio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:920–922getDataSource
public DataSource getDataSource()
获取数据来源。
Get the DataSource used for database connections.
DataSourceio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:929–931clearAllSessions
public int clearAllSessions()
清空所有会话。
Clear all sessions from the database (for testing or cleanup).
intio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:939–956truncateAllSessions
public int truncateAllSessions()
返回 truncateAllSessions 结果。
Truncate session table from the database (for testing or cleanup).
intio/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:974–983validateSessionId
protected void validateSessionId(String sessionId)
校验会话标识。
Validate a session ID format.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionId | String | 会话标识(类型 `String`)。AgentStateStore ID to validate |
IllegalArgumentException当if 会话标识是无效时抛出 `IllegalArgumentException`。if session ID is invalid
io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:991–998