rucora-skills 0.1.5

Skills system for rucora (YAML command templates)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Skill 配置模块
//!
//! 支持多种配置文件格式:YAML, TOML, JSON
//! 支持按需加载、配置验证、配置合并等功能

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// Skill 配置(支持 skill.yaml, skill.toml, skill.json)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SkillConfig {
    /// 技能基本信息
    #[serde(default)]
    pub skill: SkillMeta,

    /// 输入 Schema
    #[serde(default)]
    pub input_schema: Option<serde_json::Value>,

    /// 输出 Schema
    #[serde(default)]
    pub output_schema: Option<serde_json::Value>,

    /// 执行配置
    #[serde(default)]
    pub execution: Option<ExecutionConfig>,

    /// 触发器配置
    #[serde(default)]
    pub triggers: Vec<String>,

    /// 权限配置
    #[serde(default)]
    pub permissions: Option<PermissionsConfig>,

    /// 依赖配置
    #[serde(default)]
    pub dependencies: Option<DependenciesConfig>,

    /// 元数据
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

/// 技能元数据
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SkillMeta {
    /// 技能名称
    pub name: String,

    /// 技能描述
    #[serde(default)]
    pub description: String,

    /// 版本号
    #[serde(default = "default_version")]
    pub version: String,

    /// 作者
    #[serde(default)]
    pub author: Option<String>,

    /// 标签
    #[serde(default)]
    pub tags: Vec<String>,
}

fn default_version() -> String {
    "0.1.0".to_string()
}

/// 执行配置
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExecutionConfig {
    /// 超时时间(秒)
    #[serde(default = "default_timeout")]
    pub timeout: u64,

    /// 工作目录
    #[serde(default)]
    pub work_dir: Option<String>,

    /// 重试次数
    #[serde(default)]
    pub retries: u32,

    /// 是否缓存结果
    #[serde(default)]
    pub cache: bool,
}

fn default_timeout() -> u64 {
    30
}

/// 权限配置
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PermissionsConfig {
    /// 网络访问权限
    #[serde(default)]
    pub network: bool,

    /// 文件系统访问权限
    #[serde(default)]
    pub filesystem: bool,

    /// 允许的命令列表
    #[serde(default)]
    pub commands: Vec<String>,

    /// 允许的域名白名单
    #[serde(default)]
    pub allowed_domains: Vec<String>,

    /// 禁止的域名黑名单
    #[serde(default)]
    pub denied_domains: Vec<String>,
}

/// 依赖配置
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DependenciesConfig {
    /// 需要的二进制文件
    #[serde(default)]
    pub bins: Vec<String>,

    /// 需要的环境变量
    #[serde(default)]
    pub env: Vec<String>,

    /// 需要的 Python 包
    #[serde(default)]
    pub python_packages: Vec<String>,
}

/// 配置加载选项(按需加载)
#[derive(Debug, Clone, Default)]
pub struct ConfigLoadOptions {
    /// 是否加载基本信息
    pub load_basic: bool,
    /// 是否加载输入输出 Schema
    pub load_schema: bool,
    /// 是否加载执行配置
    pub load_execution: bool,
    /// 是否加载触发器
    pub load_triggers: bool,
    /// 是否加载权限配置
    pub load_permissions: bool,
    /// 是否加载依赖配置
    pub load_dependencies: bool,
    /// 是否加载元数据
    pub load_metadata: bool,
}

impl ConfigLoadOptions {
    /// 创建只加载基本信息的选项(用于 LLM 调用)
    pub fn for_llm() -> Self {
        Self {
            load_basic: true,
            load_schema: true,
            ..Default::default()
        }
    }

    /// 创建只加载执行配置的选项(用于技能执行)
    pub fn for_execution() -> Self {
        Self {
            load_basic: true,
            load_execution: true,
            load_permissions: true,
            load_dependencies: true,
            ..Default::default()
        }
    }

    /// 创建只加载注册信息的选项(用于技能注册)
    pub fn for_registration() -> Self {
        Self {
            load_basic: true,
            load_triggers: true,
            ..Default::default()
        }
    }

    /// 创建只加载搜索信息的选项(用于技能搜索)
    pub fn for_search() -> Self {
        Self {
            load_basic: true,
            load_triggers: true,
            ..Default::default()
        }
    }

    /// 创建加载所有信息的选项
    pub fn full() -> Self {
        Self {
            load_basic: true,
            load_schema: true,
            load_execution: true,
            load_triggers: true,
            load_permissions: true,
            load_dependencies: true,
            load_metadata: true,
        }
    }
}

/// 配置验证错误
#[derive(Debug, Clone)]
pub struct ConfigError {
    pub field: String,
    pub message: String,
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.field, self.message)
    }
}

impl std::error::Error for ConfigError {}

impl SkillConfig {
    /// 从文件加载配置(自动检测格式)
    pub fn from_file(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        let content = std::fs::read_to_string(path)?;
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");

        match ext.to_lowercase().as_str() {
            "yaml" | "yml" => Ok(serde_yaml::from_str(&content)?),
            "toml" => Ok(toml::from_str(&content)?),
            "json" => Ok(serde_json::from_str(&content)?),
            _ => Err(format!("Unsupported config format: {ext}").into()),
        }
    }

    /// 从文件加载配置(按需加载)
    pub fn from_file_with_options(
        path: &Path,
        options: &ConfigLoadOptions,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let mut config = Self::from_file(path)?;
        config.apply_options(options);
        Ok(config)
    }

    /// 应用加载选项(清理不需要的字段)
    pub fn apply_options(&mut self, options: &ConfigLoadOptions) {
        if !options.load_schema {
            self.input_schema = None;
            self.output_schema = None;
        }

        if !options.load_execution {
            self.execution = None;
        }

        if !options.load_triggers {
            self.triggers.clear();
        }

        if !options.load_permissions {
            self.permissions = None;
        }

        if !options.load_dependencies {
            self.dependencies = None;
        }

        if !options.load_metadata {
            self.metadata.clear();
        }
    }

    /// 尝试从目录加载配置(按优先级尝试不同格式)
    pub fn from_dir(dir: &Path) -> Option<Self> {
        // 优先级:skill.yaml > skill.toml > skill.json
        let formats = ["skill.yaml", "skill.toml", "skill.json"];

        for format in &formats {
            let path = dir.join(format);
            if path.exists()
                && let Ok(config) = Self::from_file(&path)
            {
                return Some(config);
            }
        }
        None
    }

    /// 从目录加载配置(按需加载)
    pub fn from_dir_with_options(dir: &Path, options: &ConfigLoadOptions) -> Option<Self> {
        let formats = ["skill.yaml", "skill.toml", "skill.json"];

        for format in &formats {
            let path = dir.join(format);
            if path.exists()
                && let Ok(mut config) = Self::from_file(&path)
            {
                config.apply_options(options);
                return Some(config);
            }
        }
        None
    }

    /// 合并另一个配置(当前配置优先)
    pub fn merge(&self, other: &Self) -> Self {
        Self {
            skill: SkillMeta {
                name: if self.skill.name.is_empty() {
                    other.skill.name.clone()
                } else {
                    self.skill.name.clone()
                },
                description: if self.skill.description.is_empty() {
                    other.skill.description.clone()
                } else {
                    self.skill.description.clone()
                },
                version: if self.skill.version.is_empty() || self.skill.version == "0.1.0" {
                    other.skill.version.clone()
                } else {
                    self.skill.version.clone()
                },
                author: self
                    .skill
                    .author
                    .clone()
                    .or_else(|| other.skill.author.clone()),
                tags: {
                    let mut tags = self.skill.tags.clone();
                    tags.extend(other.skill.tags.iter().cloned());
                    tags.sort();
                    tags.dedup();
                    tags
                },
            },
            input_schema: self
                .input_schema
                .clone()
                .or_else(|| other.input_schema.clone()),
            output_schema: self
                .output_schema
                .clone()
                .or_else(|| other.output_schema.clone()),
            execution: self.execution.clone().or_else(|| other.execution.clone()),
            triggers: {
                let mut triggers = self.triggers.clone();
                triggers.extend(other.triggers.iter().cloned());
                triggers.sort();
                triggers.dedup();
                triggers
            },
            permissions: self
                .permissions
                .clone()
                .or_else(|| other.permissions.clone()),
            dependencies: self
                .dependencies
                .clone()
                .or_else(|| other.dependencies.clone()),
            metadata: {
                let mut metadata = self.metadata.clone();
                metadata.extend(other.metadata.iter().map(|(k, v)| (k.clone(), v.clone())));
                metadata
            },
        }
    }

    /// 获取技能的显示名称
    pub fn display_name(&self) -> &str {
        if self.skill.name.is_empty() {
            "Unknown"
        } else {
            &self.skill.name
        }
    }

    /// 获取技能的描述
    pub fn display_description(&self) -> &str {
        if self.skill.description.is_empty() {
            "No description provided"
        } else {
            &self.skill.description
        }
    }

    /// 检查配置是否有效
    pub fn validate(&self) -> Result<(), Vec<ConfigError>> {
        let mut errors = Vec::new();

        // 验证 name
        if self.skill.name.is_empty() {
            errors.push(ConfigError {
                field: "skill.name".to_string(),
                message: "is required".to_string(),
            });
        } else if self.skill.name.len() < 3 || self.skill.name.len() > 50 {
            errors.push(ConfigError {
                field: "skill.name".to_string(),
                message: "must be 3-50 characters".to_string(),
            });
        } else if !self
            .skill
            .name
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
        {
            errors.push(ConfigError {
                field: "skill.name".to_string(),
                message: "must contain only lowercase letters, digits, and hyphens".to_string(),
            });
        }

        // 验证 description
        if self.skill.description.is_empty() {
            errors.push(ConfigError {
                field: "skill.description".to_string(),
                message: "is required".to_string(),
            });
        } else if self.skill.description.len() > 500 {
            errors.push(ConfigError {
                field: "skill.description".to_string(),
                message: "must be less than 500 characters".to_string(),
            });
        }

        // 验证 version
        if !self.skill.version.is_empty() && self.skill.version != "0.1.0" {
            // 简单的语义化版本验证
            let parts: Vec<&str> = self.skill.version.split('.').collect();
            if parts.len() != 3 || !parts.iter().all(|p| p.parse::<u32>().is_ok()) {
                errors.push(ConfigError {
                    field: "skill.version".to_string(),
                    message: "must follow semantic versioning (e.g., 1.0.0)".to_string(),
                });
            }
        }

        // 验证 timeout
        if let Some(exec) = &self.execution {
            if exec.timeout == 0 {
                errors.push(ConfigError {
                    field: "execution.timeout".to_string(),
                    message: "must be greater than 0".to_string(),
                });
            } else if exec.timeout > 300 {
                errors.push(ConfigError {
                    field: "execution.timeout".to_string(),
                    message: "must be less than 300 seconds".to_string(),
                });
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// 检查是否有某个标签
    pub fn has_tag(&self, tag: &str) -> bool {
        self.skill.tags.iter().any(|t| t == tag)
    }

    /// 检查是否匹配触发器
    pub fn matches_trigger(&self, keyword: &str) -> bool {
        self.triggers
            .iter()
            .any(|t| t.contains(keyword) || keyword.contains(t))
    }

    /// 获取配置的摘要信息
    pub fn summary(&self) -> String {
        format!(
            "{} v{} - {}",
            self.display_name(),
            self.skill.version,
            self.display_description()
        )
    }

    /// 转换为 JSON 字符串
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// 从 JSON 字符串加载
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_minimal_config() {
        let config = SkillConfig {
            skill: SkillMeta {
                name: "test-skill".to_string(),
                description: "Test skill".to_string(),
                ..Default::default()
            },
            ..Default::default()
        };

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_validate_missing_name() {
        let config = SkillConfig {
            skill: SkillMeta {
                name: "".to_string(),
                description: "Test".to_string(),
                ..Default::default()
            },
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_merge_configs() {
        let config1 = SkillConfig {
            skill: SkillMeta {
                name: "skill1".to_string(),
                description: "Description 1".to_string(),
                tags: vec!["tag1".to_string()],
                ..Default::default()
            },
            triggers: vec!["trigger1".to_string()],
            ..Default::default()
        };

        let config2 = SkillConfig {
            skill: SkillMeta {
                name: "skill2".to_string(),
                description: "Description 2".to_string(),
                tags: vec!["tag2".to_string()],
                ..Default::default()
            },
            triggers: vec!["trigger2".to_string()],
            ..Default::default()
        };

        let merged = config1.merge(&config2);

        assert_eq!(merged.skill.name, "skill1");
        assert!(merged.skill.tags.contains(&"tag1".to_string()));
        assert!(merged.skill.tags.contains(&"tag2".to_string()));
        assert!(merged.triggers.contains(&"trigger1".to_string()));
        assert!(merged.triggers.contains(&"trigger2".to_string()));
    }

    #[test]
    fn test_has_tag() {
        let config = SkillConfig {
            skill: SkillMeta {
                name: "test".to_string(),
                description: "Test".to_string(),
                tags: vec!["weather".to_string(), "api".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };

        assert!(config.has_tag("weather"));
        assert!(config.has_tag("api"));
        assert!(!config.has_tag("unknown"));
    }

    #[test]
    fn test_matches_trigger() {
        let config = SkillConfig {
            skill: SkillMeta {
                name: "test".to_string(),
                description: "Test".to_string(),
                ..Default::default()
            },
            triggers: vec!["天气".to_string(), "天气查询".to_string()],
            ..Default::default()
        };

        assert!(config.matches_trigger("北京天气"));
        assert!(config.matches_trigger("天气查询"));
        assert!(!config.matches_trigger("计算"));
    }
}