Files
zclaw_openfang/crates/zclaw-skills/src/registry.rs
iven aa6a9cbd84
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
feat: 新增技能编排引擎和工作流构建器组件
refactor: 统一Hands系统常量到单个源文件
refactor: 更新Hands中文名称和描述

fix: 修复技能市场在连接状态变化时重新加载
fix: 修复身份变更提案的错误处理逻辑

docs: 更新多个功能文档的验证状态和实现位置
docs: 更新Hands系统文档

test: 添加测试文件验证工作区路径
2026-03-25 08:27:25 +08:00

150 lines
4.6 KiB
Rust

//! Skill registry
//!
//! Manage loaded skills and their execution.
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use zclaw_types::{Result, SkillId};
use super::{Skill, SkillContext, SkillManifest, SkillMode, SkillResult};
use crate::loader;
use crate::runner::{PromptOnlySkill, ShellSkill};
/// Skill registry
pub struct SkillRegistry {
skills: RwLock<HashMap<SkillId, Arc<dyn Skill>>>,
manifests: RwLock<HashMap<SkillId, SkillManifest>>,
skill_dirs: RwLock<Vec<PathBuf>>,
}
impl SkillRegistry {
pub fn new() -> Self {
Self {
skills: RwLock::new(HashMap::new()),
manifests: RwLock::new(HashMap::new()),
skill_dirs: RwLock::new(Vec::new()),
}
}
/// Add a skill directory to scan
pub async fn add_skill_dir(&self, dir: PathBuf) -> Result<()> {
if !dir.exists() {
return Err(zclaw_types::ZclawError::NotFound(format!("Directory not found: {}", dir.display())));
}
{
let mut dirs = self.skill_dirs.write().await;
if !dirs.contains(&dir) {
dirs.push(dir.clone());
}
}
// Scan for skills
let skill_paths = loader::discover_skills(&dir)?;
for skill_path in skill_paths {
self.load_skill_from_dir(&skill_path).await?;
}
Ok(())
}
/// Load a skill from directory
async fn load_skill_from_dir(&self, dir: &PathBuf) -> Result<()> {
let md_path = dir.join("SKILL.md");
let toml_path = dir.join("skill.toml");
let manifest = if md_path.exists() {
loader::load_skill_md(&md_path)?
} else if toml_path.exists() {
loader::load_skill_toml(&toml_path)?
} else {
return Err(zclaw_types::ZclawError::NotFound(
format!("No SKILL.md or skill.toml found in {}", dir.display())
));
};
// Create skill instance
let skill: Arc<dyn Skill> = match &manifest.mode {
SkillMode::PromptOnly => {
let prompt = std::fs::read_to_string(&md_path).unwrap_or_default();
Arc::new(PromptOnlySkill::new(manifest.clone(), prompt))
}
SkillMode::Shell => {
let cmd = std::fs::read_to_string(dir.join("command.sh"))
.unwrap_or_else(|_| "echo 'Shell skill not configured'".to_string());
Arc::new(ShellSkill::new(manifest.clone(), cmd))
}
_ => {
let prompt = std::fs::read_to_string(&md_path).unwrap_or_default();
Arc::new(PromptOnlySkill::new(manifest.clone(), prompt))
}
};
// Register (use async write instead of blocking_write)
let mut skills = self.skills.write().await;
let mut manifests = self.manifests.write().await;
skills.insert(manifest.id.clone(), skill);
manifests.insert(manifest.id.clone(), manifest);
Ok(())
}
/// Get a skill by ID
pub async fn get(&self, id: &SkillId) -> Option<Arc<dyn Skill>> {
let skills = self.skills.read().await;
skills.get(id).cloned()
}
/// Get skill manifest
pub async fn get_manifest(&self, id: &SkillId) -> Option<SkillManifest> {
let manifests = self.manifests.read().await;
manifests.get(id).cloned()
}
/// List all skills
pub async fn list(&self) -> Vec<SkillManifest> {
let manifests = self.manifests.read().await;
manifests.values().cloned().collect()
}
/// Execute a skill
pub async fn execute(
&self,
id: &SkillId,
context: &SkillContext,
input: serde_json::Value,
) -> Result<SkillResult> {
let skill = self.get(id).await
.ok_or_else(|| zclaw_types::ZclawError::NotFound(format!("Skill not found: {}", id)))?;
skill.execute(context, input).await
}
/// Remove a skill
pub async fn remove(&self, id: &SkillId) {
let mut skills = self.skills.write().await;
let mut manifests = self.manifests.write().await;
skills.remove(id);
manifests.remove(id);
}
/// Register a skill directly
pub async fn register(&self, skill: Arc<dyn Skill>, manifest: SkillManifest) {
let mut skills = self.skills.write().await;
let mut manifests = self.manifests.write().await;
skills.insert(manifest.id.clone(), skill);
manifests.insert(manifest.id.clone(), manifest);
}
}
impl Default for SkillRegistry {
fn default() -> Self {
Self::new()
}
}