MysqlSkillRepository
public class MysqlSkillRepository implements AgentSkillRepository
io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:121–1308MySQL 技能仓库,负责持久化与读取数据。位于 io.agentscope.core.skill.repository.mysql 包,实现 AgentSkillRepository。核心成员包括 getSkill、getAllSkillNames、getAllSkills、save、delete、skillExists。
MySQL database-based implementation of AgentSkillRepository.
This implementation stores skills in MySQL 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_skills (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
description TEXT NOT NULL,
skill_content LONGTEXT NOT NULL,
source VARCHAR(255) NOT NULL,
metadata_json LONGTEXT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS agentscope_skill_resources (
id BIGINT NOT NULL,
resource_path VARCHAR(500) NOT NULL,
resource_content LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id, resource_path),
FOREIGN KEY (id) REFERENCES agentscope_skills(id) ON DELETE CASCADE
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
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 database/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
-
UTF-8 (utf8mb4) character set support for internationalization
Example usage:
`// Using simple constructor with default database/table names
DataSource dataSource = createDataSource();
MysqlSkillRepository repo = new MysqlSkillRepository(dataSource, true, true);
// Using Builder for custom configuration
MysqlSkillRepository repo = MysqlSkillRepository.builder(dataSource)
.databaseName("my_database")
.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 MysqlSkillRepository instances with custom configuration. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public MysqlSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable) | 初始化 `MysqlSkillRepository` 实例。 Create a MysqlSkillRepository with default database 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 getDatabaseName() | 获取数据库名称。 Get the database 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 | 创建用于MysqlSkillRepository的构建器。 Create a new Builder for MysqlSkillRepository. |
构造器详情Constructor Detail
MysqlSkillRepository
public MysqlSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable)
初始化 MysqlSkillRepository 实例。
Create a MysqlSkillRepository with default database and table names.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections |
createIfNotExist | boolean | 创建 If 不 Exist(类型 `boolean`)。If true, auto-create the database 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 database/tables do not exist
io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:174–183方法详情Method Detail
getSkill
public AgentSkill getSkill(String name)
获取技能。
Returns the skill.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | String | 名称(类型 `String`)。The name. |
AgentSkillio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:441–499getAllSkillNames
public List<String> getAllSkillNames()
获取 getAllSkillNames。
Returns the all skill names.
List<String>io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:501–521getAllSkills
public List<AgentSkill> getAllSkills()
获取所有技能。
Returns the all skills.
List<AgentSkill>io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:523–597save
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/mysql/MysqlSkillRepository.java:599–682delete
public boolean delete(String skillName)
删除数据。
Performs the delete operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skillName | String | 技能名称(类型 `String`)。The skill name. |
booleanio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:802–834skillExists
public boolean skillExists(String skillName)
设置该参数。
Performs the skill exists operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skillName | String | 技能名称(类型 `String`)。The skill name. |
booleanio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:858–869getRepositoryInfo
public AgentSkillRepositoryInfo getRepositoryInfo()
获取仓库信息。
Returns the repository info.
AgentSkillRepositoryInfoio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:891–895getSource
public String getSource()
获取来源。
Returns the source.
Stringio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:897–900setWriteable
public void setWriteable(boolean writeable)
设置 setWriteable。
Sets the writeable.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
writeable | boolean | writeable(类型 `boolean`)。The writeable. |
io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:902–905isWriteable
public boolean isWriteable()
判断是否 isWriteable。
Returns whether the writeable.
booleanio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:907–910close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:912–916getDatabaseName
public String getDatabaseName()
获取数据库名称。
Get the database name used for storing skills.
Stringio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:923–925getSkillsTableName
public String getSkillsTableName()
获取 getSkillsTableName。
Get the skills table name.
Stringio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:932–934getResourcesTableName
public String getResourcesTableName()
获取 getResourcesTableName。
Get the resources table name.
Stringio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:941–943getDataSource
public DataSource getDataSource()
获取数据来源。
Get the DataSource used for database connections.
DataSourceio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:950–952clearAllSkills
public int clearAllSkills()
清空所有技能。
Clear all skills from the database (for testing or cleanup).
intio/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:1057–1089builder
public static Builder builder(DataSource dataSource)
创建用于MysqlSkillRepository的构建器。
Create a new Builder for MysqlSkillRepository.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
dataSource | DataSource | 数据来源(类型 `DataSource`)。DataSource for database connections (required) |
BuilderIllegalArgumentException可能抛出 `IllegalArgumentException`。if dataSource is null
io/agentscope/core/skill/repository/mysql/MysqlSkillRepository.java:1199–1201嵌套类详情Nested Class Detail
Builder
public static class Builder构建器,用于构建并配置目标实例。位于 io.agentscope.core.skill.repository.mysql 包。核心成员包括 databaseName、skillsTableName、resourcesTableName、createIfNotExist、writeable、build。
Builder for creating MysqlSkillRepository instances with custom configuration.
This builder provides a fluent API for configuring all aspects of the repository, including database name, table names, and behavior options.