PostgresSkillRepository
public class PostgresSkillRepository implements AgentSkillRepository
io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:123–1397Postgres 技能仓库,负责持久化与读取数据。位于 io.agentscope.core.skill.repository.postgresql 包,实现 AgentSkillRepository。核心成员包括 getSkill、getAllSkillNames、getAllSkills、save、delete、skillExists。
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
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | 构建器,用于构建并配置目标实例。 Builder for creating PostgresSkillRepository instances with custom configuration. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public PostgresSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable) | 初始化 `PostgresSkillRepository` 实例。 Create a PostgresSkillRepository with default schema and table names. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public AgentSkill getSkill(String name) | 获取技能。 Returns the skill. |
public List<String> getAllSkillNames() | 获取 `getAllSkillNames`。 Returns the all skill names. |
public List<AgentSkill> getAllSkills() | 获取所有技能。 Returns the all skills. |
public boolean save(List<AgentSkill> skills, boolean force) | 保存数据到持久化存储。 Performs the save operation. |
public boolean delete(String skillName) | 删除数据。 Performs the delete operation. |
public boolean skillExists(String skillName) | 设置该参数。 Performs the skill exists operation. |
public AgentSkillRepositoryInfo getRepositoryInfo() | 获取仓库信息。 Returns the repository info. |
public String getSource() | 获取来源。 Returns the source. |
public void setWriteable(boolean writeable) | 设置 `setWriteable`。 Sets the writeable. |
public boolean isWriteable() | 判断是否 `isWriteable`。 Returns whether the writeable. |
public void close() | 关闭连接并释放相关资源。 Releases resources and connections. |
public String getSchemaName() | 获取模式名称。 Get the schema name used for storing skills. |
public String getSkillsTableName() | 获取 `getSkillsTableName`。 Get the skills table name. |
public String getResourcesTableName() | 获取 `getResourcesTableName`。 Get the resources table name. |
public DataSource getDataSource() | 获取数据来源。 Get the DataSource used for database connections. |
public int clearAllSkills() | 清空所有技能。 Clear all skills from the database (for testing or cleanup). |
public static Builder builder(DataSource dataSource)static | 创建用于PostgresSkillRepository的构建器。 Create a new Builder for PostgresSkillRepository. |
构造器详情Constructor Detail
PostgresSkillRepository
public PostgresSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable)
初始化 PostgresSkillRepository 实例。
Create a PostgresSkillRepository with default schema and table names.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。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 | boolean | writeable(类型 `boolean`)。Whether the repository supports write operations |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is nullIllegalStateException可能抛出 `IllegalStateException`。if createIfNotExist is false and schema/tables do not exist
io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:185–194方法详情Method Detail
getSkill
public AgentSkill getSkill(String name)
获取技能。
Returns the skill.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | String | 名称(类型 `String`)。The name. |
AgentSkillio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:497–555getAllSkillNames
public List<String> getAllSkillNames()
获取 getAllSkillNames。
Returns the all skill names.
List<String>io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:557–577getAllSkills
public List<AgentSkill> getAllSkills()
获取所有技能。
Returns the all skills.
List<AgentSkill>io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:579–653save
public boolean save(List<AgentSkill> skills, boolean force)
保存数据到持久化存储。
Performs the save operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skills | List<AgentSkill> | 技能(类型 `List`)。The skills. |
force | boolean | force(类型 `boolean`)。The force. |
booleanio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:655–729delete
public boolean delete(String skillName)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skillName | String | 技能名称(类型 `String`)。The skill name. |
booleanio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:881–909skillExists
public boolean skillExists(String skillName)
设置该参数。
Performs the skill exists operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skillName | String | 技能名称(类型 `String`)。The skill name. |
booleanio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:933–944getRepositoryInfo
public AgentSkillRepositoryInfo getRepositoryInfo()
获取仓库信息。
Returns the repository info.
AgentSkillRepositoryInfoio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:966–970getSource
public String getSource()
获取来源。
Returns the source.
Stringio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:972–975setWriteable
public void setWriteable(boolean writeable)
设置 setWriteable。
Sets the writeable.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
writeable | boolean | writeable(类型 `boolean`)。The writeable. |
io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:977–980isWriteable
public boolean isWriteable()
判断是否 isWriteable。
Returns whether the writeable.
booleanio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:982–985close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:987–991getSchemaName
public String getSchemaName()
获取模式名称。
Get the schema name used for storing skills.
Stringio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:998–1000getSkillsTableName
public String getSkillsTableName()
获取 getSkillsTableName。
Get the skills table name.
Stringio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:1007–1009getResourcesTableName
public String getResourcesTableName()
获取 getResourcesTableName。
Get the resources table name.
Stringio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:1016–1018getDataSource
public DataSource getDataSource()
获取数据来源。
Get the DataSource used for database connections.
DataSourceio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:1025–1027clearAllSkills
public int clearAllSkills()
清空所有技能。
Clear all skills from the database (for testing or cleanup).
intio/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:1131–1156builder
public static Builder builder(DataSource dataSource)
创建用于PostgresSkillRepository的构建器。
Create a new Builder for PostgresSkillRepository.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections (required) |
BuilderIllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is null
io/agentscope/core/skill/repository/postgresql/PostgresSkillRepository.java:1288–1290嵌套类详情Nested Class Detail
Builder
public static class Builder构建器,用于构建并配置目标实例。位于 io.agentscope.core.skill.repository.postgresql 包。核心成员包括 schemaName、skillsTableName、resourcesTableName、createIfNotExist、writeable、build。
Builder for creating PostgresSkillRepository instances with custom configuration.
This builder provides a fluent API for configuring all aspects of the repository, including schema name, table names, and behavior options.