Class MysqlSkillRepository
java.lang.Object
io.agentscope.core.skill.repository.mysql.MysqlSkillRepository
- All Implemented Interfaces:
io.agentscope.core.skill.repository.AgentSkillRepository,AutoCloseable
public class MysqlSkillRepository
extends Object
implements io.agentscope.core.skill.repository.AgentSkillRepository
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
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating MysqlSkillRepository instances with custom configuration. -
Constructor Summary
ConstructorsConstructorDescriptionMysqlSkillRepository(DataSource dataSource, boolean createIfNotExist, boolean writeable) Create a MysqlSkillRepository with default database and table names. -
Method Summary
Modifier and TypeMethodDescriptionstatic MysqlSkillRepository.Builderbuilder(DataSource dataSource) Create a new Builder for MysqlSkillRepository.intClear all skills from the database (for testing or cleanup).voidclose()booleanList<io.agentscope.core.skill.AgentSkill>Get the database name used for storing skills.Get the DataSource used for database connections.io.agentscope.core.skill.repository.AgentSkillRepositoryInfoGet the resources table name.io.agentscope.core.skill.AgentSkillGet the skills table name.booleanbooleanvoidsetWriteable(boolean writeable) booleanskillExists(String skillName)
-
Constructor Details
-
MysqlSkillRepository
Create a MysqlSkillRepository with default database and table names.This constructor uses default database name (
agentscope) and table names (agentscope_skillsandagentscope_skill_resources).- Parameters:
dataSource- DataSource for database connectionscreateIfNotExist- If true, auto-create the database 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 database/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
-
getDatabaseName
Get the database name used for storing skills.- Returns:
- the database 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 MysqlSkillRepository.Example usage:
MysqlSkillRepository repo = MysqlSkillRepository.builder(dataSource) .databaseName("my_database") .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
-