PostgresAgentStateStore
public class PostgresAgentStateStore implements AgentStateStore
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:58–831Postgres 智能体状态存储,负责持久化与读取数据。位于 io.agentscope.extensions.postgresql.state 包,实现 AgentStateStore。核心成员包括 supportsVersioning、save、getVersioned、saveIfVersion、get、getList。
PostgreSQL database-based AgentStateStore implementation.
Stores session state in PostgreSQL tables with the following structure:
CREATE TABLE IF NOT EXISTS "agentscope"."agentscope_sessions" (
session_id VARCHAR(255) NOT NULL,
state_key VARCHAR(255) NOT NULL,
item_index INT NOT NULL DEFAULT 0,
state_data TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (session_id, state_key, item_index)
);
-
Single state: stored as JSON with item_index = 0
-
List state: each item stored in a separate row with item_index = 0, 1, 2, ...
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | `PostgresAgentStateStore`的构建器。 Builder for `PostgresAgentStateStore`. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public PostgresAgentStateStore(DataSource dataSource) | 创建PostgresAgentStateStore 使用默认设置。 Creates a PostgresAgentStateStore with default settings. |
public PostgresAgentStateStore(DataSource dataSource, boolean createIfNotExist) | 初始化 `PostgresAgentStateStore` 实例。 Creates a PostgresAgentStateStore with optional auto-creation. |
public PostgresAgentStateStore(DataSource dataSource, String schemaName, String tableName, boolean createIfNotExist) | 初始化 `PostgresAgentStateStore` 实例。 Creates a PostgresAgentStateStore with custom schema/table and optional auto-creation. |
方法摘要Method Summary
构造器详情Constructor Detail
PostgresAgentStateStore
public PostgresAgentStateStore(DataSource dataSource)
创建PostgresAgentStateStore 使用默认设置。
Creates a PostgresAgentStateStore with default settings.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。The data source. |
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:91–93PostgresAgentStateStore
public PostgresAgentStateStore(DataSource dataSource, boolean createIfNotExist)
初始化 PostgresAgentStateStore 实例。
Creates a PostgresAgentStateStore with optional auto-creation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。The data source. |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。The create if not exist. |
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:101–103PostgresAgentStateStore
public PostgresAgentStateStore(DataSource dataSource, String schemaName, String tableName, boolean createIfNotExist)
初始化 PostgresAgentStateStore 实例。
Creates a PostgresAgentStateStore with custom schema/table and optional auto-creation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。The data source. |
schemaName | String | 模式名称(类型 `String`)。The schema name. |
tableName | String | table 名称(类型 `String`)。The table name. |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。The create if not exist. |
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:108–135方法详情Method Detail
supportsVersioning
public boolean supportsVersioning()
返回 supportsVersioning 结果。
Returns the supports versioning.
booleanio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:262–265save
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/postgresql/state/PostgresAgentStateStore.java:267–302getVersioned
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/postgresql/state/PostgresAgentStateStore.java:304–331saveIfVersion
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/postgresql/state/PostgresAgentStateStore.java:358–393save
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/extensions/postgresql/state/PostgresAgentStateStore.java:440–476get
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/postgresql/state/PostgresAgentStateStore.java:583–609getList
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/postgresql/state/PostgresAgentStateStore.java:611–637exists
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/postgresql/state/PostgresAgentStateStore.java:639–654delete
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/postgresql/state/PostgresAgentStateStore.java:656–674listSessionIds
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/postgresql/state/PostgresAgentStateStore.java:676–698close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:713–716truncateAllSessions
public int truncateAllSessions()
返回 truncateAllSessions 结果。
Truncate session table from the database (for testing or cleanup).
intio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:731–740getSchemaName
public String getSchemaName()
获取模式名称。
Returns the schema name.
Stringio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:742–744getTableName
public String getTableName()
获取 getTableName。
Returns the table name.
Stringio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:746–748getDataSource
public DataSource getDataSource()
获取数据来源。
Returns the data source.
DataSourceio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:750–752validateSessionId
protected void validateSessionId(String sessionId)
校验会话标识。
Validates the session id.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
sessionId | String | 会话标识(类型 `String`)。The session id. |
io/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:754–765builder
public static Builder builder(DataSource dataSource)
返回构建器实例。
Performs the builder operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。The data source. |
Builderio/agentscope/extensions/postgresql/state/PostgresAgentStateStore.java:828–830嵌套类详情Nested Class Detail
Builder
public static final class BuilderPostgresAgentStateStore的构建器。位于 io.agentscope.extensions.postgresql.state 包。核心成员包括 schemaName、tableName、createIfNotExist、build。
Builder for PostgresAgentStateStore.