Class PostgresAgentStateStore
- All Implemented Interfaces:
io.agentscope.core.state.AgentStateStore
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
Nested Classes -
Field Summary
Fields inherited from interface io.agentscope.core.state.AgentStateStore
UNVERSIONED -
Constructor Summary
ConstructorsConstructorDescriptionPostgresAgentStateStore(DataSource dataSource) Creates a PostgresAgentStateStore with default settings.PostgresAgentStateStore(DataSource dataSource, boolean createIfNotExist) Creates a PostgresAgentStateStore with optional auto-creation.PostgresAgentStateStore(DataSource dataSource, String schemaName, String tableName, boolean createIfNotExist) Creates a PostgresAgentStateStore with custom schema/table and optional auto-creation. -
Method Summary
Modifier and TypeMethodDescriptionbuilder(DataSource dataSource) voidclose()voidboolean<T extends io.agentscope.core.state.State>
Optional<T><T extends io.agentscope.core.state.State>
List<T><T extends io.agentscope.core.state.State>
io.agentscope.core.state.VersionedState<T>getVersioned(String userId, String sessionId, String key, Class<T> type) listSessionIds(String userId) voidvoidsave(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) longsaveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) booleanintTruncate session table from the database (for testing or cleanup).protected voidvalidateSessionId(String sessionId) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.agentscope.core.state.AgentStateStore
delete
-
Constructor Details
-
PostgresAgentStateStore
Creates a PostgresAgentStateStore with default settings.Uses default schema (
agentscope) and table (agentscope_sessions) and does NOT auto-create the schema or table. -
PostgresAgentStateStore
Creates a PostgresAgentStateStore with optional auto-creation.Uses default schema and table. If
createIfNotExistis true, the schema and table are created automatically if they don't exist. -
PostgresAgentStateStore
public PostgresAgentStateStore(DataSource dataSource, String schemaName, String tableName, boolean createIfNotExist) Creates a PostgresAgentStateStore with custom schema/table and optional auto-creation.
-
-
Method Details
-
supportsVersioning
public boolean supportsVersioning()- Specified by:
supportsVersioningin interfaceio.agentscope.core.state.AgentStateStore
-
save
- Specified by:
savein interfaceio.agentscope.core.state.AgentStateStore
-
getVersioned
public <T extends io.agentscope.core.state.State> io.agentscope.core.state.VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type) - Specified by:
getVersionedin interfaceio.agentscope.core.state.AgentStateStore
-
saveIfVersion
public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion) - Specified by:
saveIfVersionin interfaceio.agentscope.core.state.AgentStateStore
-
save
public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values) - Specified by:
savein interfaceio.agentscope.core.state.AgentStateStore
-
get
public <T extends io.agentscope.core.state.State> Optional<T> get(String userId, String sessionId, String key, Class<T> type) - Specified by:
getin interfaceio.agentscope.core.state.AgentStateStore
-
getList
public <T extends io.agentscope.core.state.State> List<T> getList(String userId, String sessionId, String key, Class<T> itemType) - Specified by:
getListin interfaceio.agentscope.core.state.AgentStateStore
-
exists
- Specified by:
existsin interfaceio.agentscope.core.state.AgentStateStore
-
delete
- Specified by:
deletein interfaceio.agentscope.core.state.AgentStateStore
-
listSessionIds
- Specified by:
listSessionIdsin interfaceio.agentscope.core.state.AgentStateStore
-
close
public void close()- Specified by:
closein interfaceio.agentscope.core.state.AgentStateStore
-
truncateAllSessions
public int truncateAllSessions()Truncate session table from the database (for testing or cleanup).This method clears all session records by executing a
TRUNCATE TABLEstatement on the sessions table. TRUNCATE is faster than DELETE as it resets the table without logging individual row deletions and reclaims storage space immediately.Note: In PostgreSQL,
TRUNCATE TABLEis DDL and triggers an implicit commit. For that reason, this method executes the statement directly instead of routing it throughexecuteInWriteTransaction(Connection, SqlOperation).- Returns:
- typically 0 if successful
-
getSchemaName
-
getTableName
-
getDataSource
-
validateSessionId
-
builder
-