AS AgentScopeAPI 手册 / API Reference

MysqlAgentStateStore

public class MysqlAgentStateStore implements AgentStateStore
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:69–1043

MySQL 智能体状态存储,负责持久化与读取数据。位于 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

方法Method说明Description
public boolean supportsVersioning()
返回 `supportsVersioning` 结果。
Returns the supports versioning.
public void save(String userId, String sessionId, String key, State value)
保存数据到持久化存储。
Performs the save operation.
public T extends State VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
获取 `getVersioned`。
Returns the versioned.
public long saveIfVersion(String userId, String sessionId, String key, State value, long expectedVersion)
保存If 版本。
Saves the if version.
public void save(String userId, String sessionId, String key, List<? extends State> values)
保存数据到持久化存储。
Save a list of state values with hash-based change detection.
public T extends State Optional<T> get(String userId, String sessionId, String key, Class<T> type)
按条件读取数据。
Performs the get operation.
public T extends State List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
获取列表。
Returns the list.
public boolean exists(String userId, String sessionId)
判断数据是否存在。
Performs the exists operation.
public void delete(String userId, String sessionId)
删除数据。
Performs the delete operation.
public Set<String> listSessionIds(String userId)
列出 `listSessionIds`。
Lists the session ids.
public void close()
关闭连接并释放相关资源。
Close the session and release any resources.
public String getDatabaseName()
获取数据库名称。
Get the database name used for storing sessions.
public String getTableName()
获取 `getTableName`。
Get the table name used for storing sessions.
public DataSource getDataSource()
获取数据来源。
Get the DataSource used for database connections.
public int clearAllSessions()
deprecated
清空所有会话。
Clear all sessions from the database (for testing or cleanup).
public int truncateAllSessions()
返回 `truncateAllSessions` 结果。
Truncate session table from the database (for testing or cleanup).
protected void validateSessionId(String sessionId)
校验会话标识。
Validate a session ID format.

构造器详情Constructor Detail

MysqlAgentStateStore

public MysqlAgentStateStore(DataSource dataSource)

创建MysqlAgentStateStore 使用默认设置。

Create a MysqlAgentStateStore with default settings.

参数Parameters
名称Name类型Type说明Description
dataSourceDataSource数据来源(类型 `DataSource`)。DataSource for database connections
抛出Throws
  • IllegalArgumentException 可能抛出 `IllegalArgumentException`。if dataSource is null
  • IllegalStateException 可能抛出 `IllegalStateException`。if database or table does not exist
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:111–113

MysqlAgentStateStore

public MysqlAgentStateStore(DataSource dataSource, boolean createIfNotExist)

初始化 MysqlAgentStateStore 实例。

Create a MysqlAgentStateStore with optional auto-creation of database and table.

参数Parameters
名称Name类型Type说明Description
dataSourceDataSource数据来源(类型 `DataSource`)。DataSource for database connections
createIfNotExistboolean创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing
抛出Throws
  • IllegalArgumentException 可能抛出 `IllegalArgumentException`。if dataSource is null
  • IllegalStateException 可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:128–130

MysqlAgentStateStore

public MysqlAgentStateStore(DataSource dataSource, String databaseName, String tableName, boolean createIfNotExist)

初始化 MysqlAgentStateStore 实例。

Create a MysqlAgentStateStore with custom database name, table name, and optional auto-creation.

参数Parameters
名称Name类型Type说明Description
dataSourceDataSource数据来源(类型 `DataSource`)。DataSource for database connections
databaseNameString数据库名称(类型 `String`)。Custom database name (uses default if null or empty)
tableNameStringtable 名称(类型 `String`)。Custom table name (uses default if null or empty)
createIfNotExistboolean创建 If 不 Exist(类型 `boolean`)。If true, auto-create database and table; if false, require existing
抛出Throws
  • IllegalArgumentException 可能抛出 `IllegalArgumentException`。if dataSource is null
  • IllegalStateException 可能抛出 `IllegalStateException`。if createIfNotExist is false and database/table does not exist
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:146–179

方法详情Method Detail

supportsVersioning

public boolean supportsVersioning()

返回 supportsVersioning 结果。

Returns the supports versioning.

返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:357–360

save

public void save(String userId, String sessionId, String key, State value)

保存数据到持久化存储。

Performs the save operation.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
keyString键(类型 `String`)。The key.
valueState值(类型 `State`)。The value.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:362–394

getVersioned

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

获取 getVersioned。

Returns the versioned.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
keyString键(类型 `String`)。The key.
typeClass<T>类型(类型 `Class`)。The type.
返回Returns VersionedState<T>
返回VersionedState。Returns versioned state.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:396–427

saveIfVersion

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

保存If 版本。

Saves the if version.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
keyString键(类型 `String`)。The key.
valueState值(类型 `State`)。The value.
expectedVersionlongexpected 版本(类型 `long`)。The expected version.
返回Returns long
返回数值结果。Returns a numeric result.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:454–489

save

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

保存数据到持久化存储。

Save a list of state values with hash-based change detection.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。the user identifier
sessionIdString会话标识(类型 `String`)。the session identifier
keyString键(类型 `String`)。the state key (e.g., "memory_messages")
valuesList<? extends State>状态值到保存的列出。the list of state values to save
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:555–591

get

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

按条件读取数据。

Performs the get operation.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
keyString键(类型 `String`)。The key.
typeClass<T>类型(类型 `Class`)。The type.
返回Returns Optional<T>
返回可选。Returns optional.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:744–774

getList

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

获取列表。

Returns the list.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
keyString键(类型 `String`)。The key.
itemTypeClass<T>条目类型(类型 `Class`)。The item type.
返回Returns List<T>
返回列出。Returns list.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:776–807

exists

public boolean exists(String userId, String sessionId)

判断数据是否存在。

Performs the exists operation.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
返回Returns boolean
返回布尔值,表示判断结果。Returns a boolean result.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:809–827

delete

public void delete(String userId, String sessionId)

删除数据。

Performs the delete operation.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
sessionIdString会话标识(类型 `String`)。The session id.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:829–848

listSessionIds

public Set<String> listSessionIds(String userId)

列出 listSessionIds。

Lists the session ids.

参数Parameters
名称Name类型Type说明Description
userIdString用户标识(类型 `String`)。The user id.
返回Returns Set<String>
返回设置。Returns set.
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:850–875

close

public void close()

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

Close the session and release any resources.

源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:901–904

getDatabaseName

public String getDatabaseName()

获取数据库名称。

Get the database name used for storing sessions.

返回Returns String
数据库名称。The database name
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:911–913

getTableName

public String getTableName()

获取 getTableName。

Get the table name used for storing sessions.

返回Returns String
返回字符串结果。The table name
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:920–922

getDataSource

public DataSource getDataSource()

获取数据来源。

Get the DataSource used for database connections.

返回Returns DataSource
DataSource 实例。The DataSource instance
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:929–931

clearAllSessions

public int clearAllSessions()
deprecated

清空所有会话。

Clear all sessions from the database (for testing or cleanup).

已弃用。Deprecated. 已弃用,请改用受支持的替代 API。Use `#truncateAllSessions()` instead
返回Returns int
返回数值结果。Number of rows deleted
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:939–956

truncateAllSessions

public int truncateAllSessions()

返回 truncateAllSessions 结果。

Truncate session table from the database (for testing or cleanup).

返回Returns int
返回数值结果。typically 0 if successful
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:974–983

validateSessionId

protected void validateSessionId(String sessionId)

校验会话标识。

Validate a session ID format.

参数Parameters
名称Name类型Type说明Description
sessionIdString会话标识(类型 `String`)。AgentStateStore ID to validate
抛出Throws
  • IllegalArgumentException 当if 会话标识是无效时抛出 `IllegalArgumentException`。if session ID is invalid
源码Source io/agentscope/extensions/mysql/state/MysqlAgentStateStore.java:991–998

继承成员Inherited Members

继承自Members inherited from AgentStateStore
基于官方固定版本源码静态生成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.