Class PostgresSkillRepository
java.lang.Object
io.agentscope.core.skill.repository.postgresql.PostgresSkillRepository
- All Implemented Interfaces:
io.agentscope.core.skill.repository.AgentSkillRepository,AutoCloseable
public class PostgresSkillRepository
extends Object
implements io.agentscope.core.skill.repository.AgentSkillRepository
PostgreSQL database-based implementation of AgentSkillRepository.
This implementation stores skills in PostgreSQL database tables with the following structure:
- Skills table: stores core lookup fields (
name,description), skill content, source, and optionally the full metadata tree inmetadata_json - Resources table: stores skill resources (
id,resource_path,resource_content)
Table schema for newly created tables (createIfNotExist=true):
CREATE TABLE IF NOT EXISTS "agentscope"."agentscope_skills" (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
description TEXT NOT NULL,
skill_content TEXT NOT NULL,
source VARCHAR(255) NOT NULL,
metadata_json TEXT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS "agentscope"."agentscope_skill_resources" (
id BIGINT NOT NULL,
resource_path VARCHAR(500) NOT NULL,
resource_content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id, resource_path),
FOREIGN KEY (id) REFERENCES "agentscope"."agentscope_skills"(id) ON DELETE CASCADE
);
Compatibility behavior:
- New tables created by this repository include
metadata_json - Existing tables are not auto-migrated with
ALTER TABLE - When
metadata_jsonexists, full skill metadata is persisted and restored - When
metadata_jsondoes not exist, the repository falls back to the legacy schema and only round-tripsnameanddescription
Features:
- Automatic schema/table creation when
createIfNotExist=true - Runtime compatibility detection for legacy and new schemas
- Full CRUD operations for skills and their resources
- SQL injection prevention through parameterized queries
- Transaction support for atomic operations
- PostgreSQL schema-based storage selected by JDBC URL plus schema configuration
Example usage:
// Using simple constructor with default schema/table names
DataSource dataSource = createDataSource();
PostgresSkillRepository repo = new PostgresSkillRepository(dataSource, true, true);
// Using Builder for custom configuration
PostgresSkillRepository repo = PostgresSkillRepository.builder(dataSource)
.schemaName("my_schema")
.skillsTableName("my_skills")
.resourcesTableName("my_resources")
.createIfNotExist(true)
.writeable(true)
.build();
// Save a skill
AgentSkill skill = new AgentSkill("my-skill", "Description", "Content", resources);
repo.save(List.of(skill), false);
// Get a skill
AgentSkill loaded = repo.getSkill("my-skill");
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating PostgresSkillRepository instances with custom configuration. -
Constructor Summary
ConstructorsConstructorDescriptionPostgresSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable) Create a PostgresSkillRepository with default schema and table names. -
Method Summary
Modifier and TypeMethodDescriptionbuilder(DataSource dataSource) Create a new Builder for PostgresSkillRepository.intClear all skills from the database (for testing or cleanup).voidclose()booleanList<io.agentscope.core.skill.AgentSkill>Get the DataSource used for database connections.io.agentscope.core.skill.repository.AgentSkillRepositoryInfoGet the resources table name.Get the schema name used for storing skills.io.agentscope.core.skill.AgentSkillGet the skills table name.booleanbooleanvoidsetWriteable(boolean writeable) booleanskillExists(String skillName)
-
Constructor Details
-
PostgresSkillRepository
Create a PostgresSkillRepository with default schema and table names.This constructor uses default schema name (
agentscope) and table names (agentscope_skillsandagentscope_skill_resources).- Parameters:
dataSource- DataSource for database connectionscreateIfNotExist- If true, auto-create the schema and tables for new deployments; if false, require existing schema. Existing tables are not auto-migrated to addmetadata_jsonwriteable- Whether the repository supports write operations- Throws:
IllegalArgumentException- if dataSource is nullIllegalStateException- if createIfNotExist is false and schema/tables do not exist
-
-
Method Details
-
getSkill
- Specified by:
getSkillin interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
getAllSkillNames
- Specified by:
getAllSkillNamesin interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
getAllSkills
- Specified by:
getAllSkillsin interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
save
- Specified by:
savein interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
delete
- Specified by:
deletein interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
skillExists
- Specified by:
skillExistsin interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
getRepositoryInfo
public io.agentscope.core.skill.repository.AgentSkillRepositoryInfo getRepositoryInfo()- Specified by:
getRepositoryInfoin interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
getSource
- Specified by:
getSourcein interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
setWriteable
public void setWriteable(boolean writeable) - Specified by:
setWriteablein interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
isWriteable
public boolean isWriteable()- Specified by:
isWriteablein interfaceio.agentscope.core.skill.repository.AgentSkillRepository
-
close
public void close()- Specified by:
closein interfaceio.agentscope.core.skill.repository.AgentSkillRepository- Specified by:
closein interfaceAutoCloseable
-
getSchemaName
Get the schema name used for storing skills.- Returns:
- the schema name
-
getSkillsTableName
Get the skills table name.- Returns:
- the skills table name
-
getResourcesTableName
Get the resources table name.- Returns:
- the resources table name
-
getDataSource
Get the DataSource used for database connections.- Returns:
- the DataSource instance
-
clearAllSkills
public int clearAllSkills()Clear all skills from the database (for testing or cleanup).Resources are deleted automatically via ON DELETE CASCADE when skills are deleted.
- Returns:
- the number of skills deleted
-
builder
Create a new Builder for PostgresSkillRepository.Example usage:
PostgresSkillRepository repo = PostgresSkillRepository.builder(dataSource) .schemaName("my_schema") .skillsTableName("my_skills") .resourcesTableName("my_resources") .createIfNotExist(true) .writeable(true) .build();- Parameters:
dataSource- DataSource for database connections (required)- Returns:
- a new Builder instance
- Throws:
IllegalArgumentException- if dataSource is null
-