fix(skills): auto-sync locally installed skills to database

Add automatic synchronization in get_skills command:
- Detect locally installed skills in ~/.claude/skills/
- Auto-add to database if not already tracked
- Ensures existing skills are recognized on first launch

This fixes the issue where user's existing skills were not
imported into the database on initial application run.
This commit is contained in:
YoVinchen
2025-11-23 16:25:55 +08:00
parent 3faf22f1c9
commit edc71efe4c

View File

@@ -15,11 +15,32 @@ pub async fn get_skills(
) -> Result<Vec<Skill>, String> { ) -> Result<Vec<Skill>, String> {
let repos = app_state.db.get_skill_repos().map_err(|e| e.to_string())?; let repos = app_state.db.get_skill_repos().map_err(|e| e.to_string())?;
service let skills = service
.0 .0
.list_skills(repos) .list_skills(repos)
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())?;
// 自动同步本地已安装的 skills 到数据库
// 这样用户在首次运行时,已有的 skills 会被自动记录
let existing_states = app_state.db.get_skills().unwrap_or_default();
for skill in &skills {
if skill.installed && !existing_states.contains_key(&skill.directory) {
// 本地有该 skill但数据库中没有记录自动添加
if let Err(e) = app_state.db.update_skill_state(
&skill.directory,
&SkillState {
installed: true,
installed_at: Utc::now(),
},
) {
log::warn!("同步本地 skill {} 状态到数据库失败: {}", skill.directory, e);
}
}
}
Ok(skills)
} }
#[tauri::command] #[tauri::command]