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 in metadata_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_json exists, full skill metadata is persisted and restored
  • When metadata_json does not exist, the repository falls back to the legacy schema and only round-trips name and description

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");
 
  • Constructor Details

    • PostgresSkillRepository

      public PostgresSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable)
      Create a PostgresSkillRepository with default schema and table names.

      This constructor uses default schema name (agentscope) and table names (agentscope_skills and agentscope_skill_resources).

      Parameters:
      dataSource - DataSource for database connections
      createIfNotExist - If true, auto-create the schema and tables for new deployments; if false, require existing schema. Existing tables are not auto-migrated to add metadata_json
      writeable - Whether the repository supports write operations
      Throws:
      IllegalArgumentException - if dataSource is null
      IllegalStateException - if createIfNotExist is false and schema/tables do not exist
  • Method Details

    • getSkill

      public io.agentscope.core.skill.AgentSkill getSkill(String name)
      Specified by:
      getSkill in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • getAllSkillNames

      public List<String> getAllSkillNames()
      Specified by:
      getAllSkillNames in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • getAllSkills

      public List<io.agentscope.core.skill.AgentSkill> getAllSkills()
      Specified by:
      getAllSkills in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • save

      public boolean save(List<io.agentscope.core.skill.AgentSkill> skills, boolean force)
      Specified by:
      save in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • delete

      public boolean delete(String skillName)
      Specified by:
      delete in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • skillExists

      public boolean skillExists(String skillName)
      Specified by:
      skillExists in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • getRepositoryInfo

      public io.agentscope.core.skill.repository.AgentSkillRepositoryInfo getRepositoryInfo()
      Specified by:
      getRepositoryInfo in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • getSource

      public String getSource()
      Specified by:
      getSource in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • setWriteable

      public void setWriteable(boolean writeable)
      Specified by:
      setWriteable in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • isWriteable

      public boolean isWriteable()
      Specified by:
      isWriteable in interface io.agentscope.core.skill.repository.AgentSkillRepository
    • close

      public void close()
      Specified by:
      close in interface io.agentscope.core.skill.repository.AgentSkillRepository
      Specified by:
      close in interface AutoCloseable
    • getSchemaName

      public String getSchemaName()
      Get the schema name used for storing skills.
      Returns:
      the schema name
    • getSkillsTableName

      public String getSkillsTableName()
      Get the skills table name.
      Returns:
      the skills table name
    • getResourcesTableName

      public String getResourcesTableName()
      Get the resources table name.
      Returns:
      the resources table name
    • getDataSource

      public DataSource 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

      public static PostgresSkillRepository.Builder builder(DataSource dataSource)
      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