From 02cb5dfc310d39bde3db81d78b306c882a64d660 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 7 Apr 2026 07:37:39 +0800 Subject: [PATCH] refactor(agent): optimize database-operation skill to read DB info from system prompt --- skills/database-operation/SKILL.md | 74 ++++++++++++------------------ 1 file changed, 29 insertions(+), 45 deletions(-) diff --git a/skills/database-operation/SKILL.md b/skills/database-operation/SKILL.md index 28a4e3f9..f4415392 100644 --- a/skills/database-operation/SKILL.md +++ b/skills/database-operation/SKILL.md @@ -2,8 +2,9 @@ name: database-operation description: >- Use this skill when you need to execute SQL against the MoviePilot database. - This skill guides you through connecting to the database (automatically detecting SQLite or PostgreSQL), - executing SQL statements, and interpreting the results. Applicable scenarios include: + This skill guides you through connecting to the database and executing SQL statements. + The database type (SQLite or PostgreSQL) and connection details are provided in the system prompt . + Applicable scenarios include: 1) The user asks about data statistics, counts, or aggregations that existing tools don't cover; 2) The user wants to inspect, modify, or fix raw database records; 3) The user asks to clean up data, update records, or perform database maintenance; @@ -13,93 +14,76 @@ allowed-tools: execute_command read_file # Database Query (数据库查询) -This skill guides you through executing SQL against the MoviePilot database. The system automatically detects whether the database is **SQLite** or **PostgreSQL** and adapts the connection method accordingly. Both read and write operations are supported. +This skill guides you through executing SQL against the MoviePilot database. Both read and write operations are supported. ## Prerequisites You need the following tools: - `execute_command` - Execute shell commands to run database queries -- `read_file` - Read configuration files to determine database type -## Workflow +## Getting Database Connection Info -### Step 1: Detect Database Type +The system prompt `` section already contains all the database connection details you need: +- **数据库类型** — `sqlite` or `postgresql` +- **数据库** — Full connection info: + - For SQLite: the database file path, e.g. `SQLite (/config/db/moviepilot.db)` + - For PostgreSQL: the connection string, e.g. `PostgreSQL (user:password@host:port/database)` -Read the environment configuration to determine the database type. The database type is controlled by the `DB_TYPE` environment variable (defaults to `sqlite`). +**Do NOT run any detection commands.** Extract the database type and connection details directly from ``. -Use `execute_command` to check: +## Executing Queries + +### SQLite Mode + +Extract the database file path from `` (the path inside the parentheses after `SQLite`). + +Use `execute_command` to run queries: ```bash -echo $DB_TYPE -``` - -- If the result is empty or `sqlite` → **SQLite** mode -- If the result is `postgresql` → **PostgreSQL** mode - -### Step 2: Connect and Execute Query - -#### SQLite Mode - -The SQLite database file is located at `${CONFIG_PATH}/user.db` (default: `/config/user.db`). - -Use `execute_command` to execute SQL queries: - -```bash -sqlite3 -header -column /config/user.db "YOUR SQL QUERY HERE;" +sqlite3 -header -column "YOUR SQL QUERY HERE;" ``` For JSON-formatted output (easier to parse): ```bash -sqlite3 -json /config/user.db "YOUR SQL QUERY HERE;" +sqlite3 -json "YOUR SQL QUERY HERE;" ``` **List all tables:** ```bash -sqlite3 -header -column /config/user.db "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" +sqlite3 -header -column "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" ``` **View table schema:** ```bash -sqlite3 /config/user.db ".schema tablename" +sqlite3 ".schema tablename" ``` -#### PostgreSQL Mode +### PostgreSQL Mode -The PostgreSQL connection parameters are configured via environment variables: -- `DB_POSTGRESQL_HOST` (default: `127.0.0.1`) -- `DB_POSTGRESQL_PORT` (default: `5432`) -- `DB_POSTGRESQL_DATABASE` (default: `moviepilot`) -- `DB_POSTGRESQL_USERNAME` (default: `moviepilot`) -- `DB_POSTGRESQL_PASSWORD` (default: `moviepilot`) +Extract the connection parameters from `` (parse `user:password@host:port/database` from the parentheses after `PostgreSQL`). -Use `execute_command` to execute SQL queries via `psql`: +Use `execute_command` to run queries via `psql`: ```bash -PGPASSWORD=$DB_POSTGRESQL_PASSWORD psql -h $DB_POSTGRESQL_HOST -p $DB_POSTGRESQL_PORT -U $DB_POSTGRESQL_USERNAME -d $DB_POSTGRESQL_DATABASE -c "YOUR SQL QUERY HERE;" -``` - -If environment variables are not set, use the defaults: - -```bash -PGPASSWORD=moviepilot psql -h localhost -p 5432 -U moviepilot -d moviepilot -c "YOUR SQL QUERY HERE;" +PGPASSWORD= psql -h -p -U -d -c "YOUR SQL QUERY HERE;" ``` **List all tables:** ```bash -PGPASSWORD=$DB_POSTGRESQL_PASSWORD psql -h $DB_POSTGRESQL_HOST -p $DB_POSTGRESQL_PORT -U $DB_POSTGRESQL_USERNAME -d $DB_POSTGRESQL_DATABASE -c "SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename;" +PGPASSWORD= psql -h -p -U -d -c "SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename;" ``` **View table schema:** ```bash -PGPASSWORD=$DB_POSTGRESQL_PASSWORD psql -h $DB_POSTGRESQL_HOST -p $DB_POSTGRESQL_PORT -U $DB_POSTGRESQL_USERNAME -d $DB_POSTGRESQL_DATABASE -c "\d tablename" +PGPASSWORD= psql -h -p -U -d -c "\d tablename" ``` -### Step 3: Interpret Results +## Interpret Results After executing the query, analyze the results and present them in a clear, user-friendly format. Use aggregation, sorting, and filtering as needed.