cc-agent-sdk 0.1.7

claude agent sdk
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
//! Type definitions for the Skills system

use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;

/// Metadata for a Skill
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SkillMetadata {
    pub id: String,
    pub name: String,
    pub description: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<String>,
    #[serde(default)]
    pub dependencies: Vec<String>,
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Resources associated with a Skill
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct SkillResources {
    #[serde(default)]
    pub folders: Vec<PathBuf>,
    #[serde(default)]
    pub tools: Vec<String>,
    #[serde(default)]
    pub tests: Vec<String>,
}

impl SkillResources {
    /// Scan folders and return a list of all files found
    ///
    /// This method recursively scans all folders configured in this SkillResources
    /// and returns a list of all file paths found. Invalid or inaccessible folders
    /// are skipped with warnings logged.
    ///
    /// # Returns
    /// A vector of PathBuf representing all files found in the configured folders
    ///
    /// # Examples
    /// ```no_run
    /// use claude_agent_sdk::skills::SkillResources;
    ///
    /// let resources = SkillResources {
    ///     folders: vec!["./resources".into()],
    ///     ..Default::default()
    /// };
    ///
    /// let files = resources.scan_folders().unwrap();
    /// for file in files {
    ///     println!("Found resource: {:?}", file);
    /// }
    /// # Ok::<(), std::io::Error>(())
    /// ```
    pub fn scan_folders(&self) -> io::Result<Vec<PathBuf>> {
        let mut all_files = Vec::new();

        for folder in &self.folders {
            if !folder.exists() {
                tracing::warn!("Resource folder does not exist: {:?}", folder);
                continue;
            }

            if !folder.is_dir() {
                tracing::warn!("Resource path is not a directory: {:?}", folder);
                continue;
            }

            self.scan_folder_recursive(folder, &mut all_files)?;
        }

        Ok(all_files)
    }

    /// Recursively scan a folder and collect all file paths
    fn scan_folder_recursive(&self, dir: &PathBuf, files: &mut Vec<PathBuf>) -> io::Result<()> {
        let entries = fs::read_dir(dir)?;

        for entry in entries {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() {
                self.scan_folder_recursive(&path, files)?;
            } else if path.is_file() {
                files.push(path);
            }
        }

        Ok(())
    }

    /// Validate that all configured folders exist and are accessible
    ///
    /// # Returns
    /// Ok(()) if all folders are valid, Err otherwise with details about invalid folders
    ///
    /// # Examples
    /// ```no_run
    /// use claude_agent_sdk::skills::SkillResources;
    ///
    /// let resources = SkillResources {
    ///     folders: vec!["./resources".into()],
    ///     ..Default::default()
    /// };
    ///
    /// match resources.validate_folders() {
    ///     Ok(_) => println!("All folders are valid"),
    ///     Err(e) => eprintln!("Invalid folders: {}", e),
    /// }
    /// ```
    pub fn validate_folders(&self) -> io::Result<()> {
        let mut invalid_folders = Vec::new();

        for folder in &self.folders {
            if !folder.exists() {
                invalid_folders.push(format!("{:?} does not exist", folder));
            } else if !folder.is_dir() {
                invalid_folders.push(format!("{:?} is not a directory", folder));
            }
        }

        if !invalid_folders.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("Invalid folders: {}", invalid_folders.join(", ")),
            ));
        }

        Ok(())
    }

    /// Add a folder to the resources
    ///
    /// # Examples
    /// ```
    /// use claude_agent_sdk::skills::SkillResources;
    ///
    /// let mut resources = SkillResources::default();
    /// resources.add_folder("./resources");
    /// assert_eq!(resources.folders.len(), 1);
    /// ```
    pub fn add_folder<P: AsRef<std::path::Path>>(&mut self, path: P) {
        let path = path.as_ref().to_path_buf();
        if !self.folders.contains(&path) {
            self.folders.push(path);
        }
    }

    /// Add a tool to the resources
    ///
    /// # Examples
    /// ```
    /// use claude_agent_sdk::skills::SkillResources;
    ///
    /// let mut resources = SkillResources::default();
    /// resources.add_tool("search".to_string());
    /// assert_eq!(resources.tools.len(), 1);
    /// ```
    pub fn add_tool(&mut self, tool: String) {
        if !self.tools.contains(&tool) {
            self.tools.push(tool);
        }
    }

    /// Add a test to the resources
    ///
    /// # Examples
    /// ```
    /// use claude_agent_sdk::skills::SkillResources;
    ///
    /// let mut resources = SkillResources::default();
    /// resources.add_test("test_basic_functionality".to_string());
    /// assert_eq!(resources.tests.len(), 1);
    /// ```
    pub fn add_test(&mut self, test: String) {
        if !self.tests.contains(&test) {
            self.tests.push(test);
        }
    }
}

/// Input for skill execution
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SkillInput {
    #[serde(default)]
    pub params: serde_json::Value,
}

/// Status of a skill
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SkillStatus {
    Ready,
    Running,
    Completed,
    Failed,
    Disabled,
}

/// A complete Skill package
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillPackage {
    pub metadata: SkillMetadata,
    pub instructions: String,
    #[serde(default)]
    pub scripts: Vec<String>,
    #[serde(default)]
    pub resources: SkillResources,
}

impl SkillPackage {
    /// Save the skill package to a file in JSON format
    pub fn save_to_file<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()> {
        let json = serde_json::to_string_pretty(self)
            .map_err(io::Error::other)?;

        let mut file = fs::File::create(path)?;
        file.write_all(json.as_bytes())?;
        Ok(())
    }

    /// Load a skill package from a file
    pub fn load_from_file<P: AsRef<std::path::Path>>(path: P) -> io::Result<Self> {
        let content = fs::read_to_string(path)?;
        let package: SkillPackage = serde_json::from_str(&content)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
        Ok(package)
    }

    /// Save the skill package to a file in YAML format (requires yaml feature)
    #[cfg(feature = "yaml")]
    pub fn save_to_yaml<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()> {
        let yaml =
            serde_yaml::to_string(self).map_err(io::Error::other)?;

        let mut file = fs::File::create(path)?;
        file.write_all(yaml.as_bytes())?;
        Ok(())
    }

    /// Load a skill package from a YAML file (requires yaml feature)
    #[cfg(feature = "yaml")]
    pub fn load_from_yaml<P: AsRef<std::path::Path>>(path: P) -> io::Result<Self> {
        let content = fs::read_to_string(path)?;
        let package: SkillPackage = serde_yaml::from_str(&content)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
        Ok(package)
    }
}

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

    #[test]
    fn test_skill_metadata_creation() {
        let metadata = SkillMetadata {
            id: "test-skill".to_string(),
            name: "Test Skill".to_string(),
            description: "A test skill".to_string(),
            version: "1.0.0".to_string(),
            author: Some("Test Author".to_string()),
            dependencies: vec!["dep1".to_string(), "dep2".to_string()],
            tags: vec!["test".to_string(), "example".to_string()],
        };

        assert_eq!(metadata.id, "test-skill");
        assert_eq!(metadata.name, "Test Skill");
        assert_eq!(metadata.version, "1.0.0");
        assert_eq!(metadata.author, Some("Test Author".to_string()));
        assert_eq!(metadata.dependencies.len(), 2);
        assert_eq!(metadata.tags.len(), 2);
    }

    #[test]
    fn test_skill_resources_default() {
        let resources = SkillResources::default();
        assert!(resources.folders.is_empty());
        assert!(resources.tools.is_empty());
        assert!(resources.tests.is_empty());
    }

    #[test]
    fn test_skill_resources_add_folder() {
        let mut resources = SkillResources::default();
        resources.add_folder("./test");
        assert_eq!(resources.folders.len(), 1);

        // Test duplicate prevention
        resources.add_folder("./test");
        assert_eq!(resources.folders.len(), 1);
    }

    #[test]
    fn test_skill_resources_add_tool() {
        let mut resources = SkillResources::default();
        resources.add_tool("search".to_string());
        assert_eq!(resources.tools.len(), 1);

        // Test duplicate prevention
        resources.add_tool("search".to_string());
        assert_eq!(resources.tools.len(), 1);
    }

    #[test]
    fn test_skill_resources_add_test() {
        let mut resources = SkillResources::default();
        resources.add_test("test_basic".to_string());
        assert_eq!(resources.tests.len(), 1);

        // Test duplicate prevention
        resources.add_test("test_basic".to_string());
        assert_eq!(resources.tests.len(), 1);
    }

    #[test]
    fn test_skill_package_creation() {
        let package = SkillPackage {
            metadata: SkillMetadata {
                id: "test-skill".to_string(),
                name: "Test Skill".to_string(),
                description: "A test skill".to_string(),
                version: "1.0.0".to_string(),
                author: Some("Test Author".to_string()),
                dependencies: vec![],
                tags: vec![],
            },
            instructions: "Test instructions".to_string(),
            scripts: vec![],
            resources: SkillResources::default(),
        };

        assert_eq!(package.metadata.id, "test-skill");
        assert_eq!(package.instructions, "Test instructions");
        assert!(package.scripts.is_empty());
    }

    #[cfg(feature = "yaml")]
    #[test]
    fn test_skill_package_yaml_serialization() {
        let package = SkillPackage {
            metadata: SkillMetadata {
                id: "test-skill".to_string(),
                name: "Test Skill".to_string(),
                description: "A test skill for YAML serialization".to_string(),
                version: "1.0.0".to_string(),
                author: Some("Test Author".to_string()),
                dependencies: vec!["dep1".to_string()],
                tags: vec!["test".to_string(), "yaml".to_string()],
            },
            instructions: "Test instructions for YAML".to_string(),
            scripts: vec!["script1.sh".to_string()],
            resources: SkillResources {
                folders: vec!["./resources".into()],
                tools: vec!["search".to_string()],
                tests: vec!["test_basic".to_string()],
            },
        };

        // Test serialization
        let yaml = serde_yaml::to_string(&package).unwrap();
        assert!(yaml.contains("test-skill"));
        assert!(yaml.contains("Test Skill"));
        assert!(yaml.contains("1.0.0"));

        // Test deserialization
        let deserialized: SkillPackage = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(deserialized.metadata.id, package.metadata.id);
        assert_eq!(deserialized.metadata.name, package.metadata.name);
        assert_eq!(deserialized.metadata.version, package.metadata.version);
        assert_eq!(deserialized.metadata.author, package.metadata.author);
        assert_eq!(
            deserialized.metadata.dependencies,
            package.metadata.dependencies
        );
        assert_eq!(deserialized.metadata.tags, package.metadata.tags);
        assert_eq!(deserialized.instructions, package.instructions);
        assert_eq!(deserialized.scripts, package.scripts);
        assert_eq!(deserialized.resources.folders, package.resources.folders);
        assert_eq!(deserialized.resources.tools, package.resources.tools);
        assert_eq!(deserialized.resources.tests, package.resources.tests);
    }

    #[cfg(feature = "yaml")]
    #[test]
    fn test_skill_package_yaml_save_and_load() {
        let temp_dir = std::env::temp_dir();
        let yaml_path = temp_dir.join("test_skill.yaml");

        let original_package = SkillPackage {
            metadata: SkillMetadata {
                id: "yaml-test-skill".to_string(),
                name: "YAML Test Skill".to_string(),
                description: "Testing YAML save and load".to_string(),
                version: "2.0.0".to_string(),
                author: Some("YAML Test Author".to_string()),
                dependencies: vec!["yaml-dep".to_string()],
                tags: vec!["yaml-test".to_string()],
            },
            instructions: "YAML test instructions".to_string(),
            scripts: vec!["yaml_script.sh".to_string()],
            resources: SkillResources {
                folders: vec![temp_dir.join("yaml_resources")],
                tools: vec!["yaml-tool".to_string()],
                tests: vec!["yaml_test".to_string()],
            },
        };

        // Save to YAML file
        original_package.save_to_yaml(&yaml_path).unwrap();
        assert!(yaml_path.exists());

        // Load from YAML file
        let loaded_package = SkillPackage::load_from_yaml(&yaml_path).unwrap();

        // Verify all fields match
        assert_eq!(loaded_package.metadata.id, original_package.metadata.id);
        assert_eq!(loaded_package.metadata.name, original_package.metadata.name);
        assert_eq!(
            loaded_package.metadata.description,
            original_package.metadata.description
        );
        assert_eq!(
            loaded_package.metadata.version,
            original_package.metadata.version
        );
        assert_eq!(
            loaded_package.metadata.author,
            original_package.metadata.author
        );
        assert_eq!(
            loaded_package.metadata.dependencies,
            original_package.metadata.dependencies
        );
        assert_eq!(loaded_package.metadata.tags, original_package.metadata.tags);
        assert_eq!(loaded_package.instructions, original_package.instructions);
        assert_eq!(loaded_package.scripts, original_package.scripts);

        // Clean up
        std::fs::remove_file(&yaml_path).unwrap();
    }

    #[cfg(feature = "yaml")]
    #[test]
    fn test_skill_package_yaml_with_optional_fields() {
        let package = SkillPackage {
            metadata: SkillMetadata {
                id: "minimal-skill".to_string(),
                name: "Minimal Skill".to_string(),
                description: "Minimal test skill".to_string(),
                version: "1.0.0".to_string(),
                author: None,
                dependencies: vec![],
                tags: vec![],
            },
            instructions: "Minimal instructions".to_string(),
            scripts: vec![],
            resources: SkillResources::default(),
        };

        let yaml = serde_yaml::to_string(&package).unwrap();
        let deserialized: SkillPackage = serde_yaml::from_str(&yaml).unwrap();

        assert_eq!(deserialized.metadata.author, None);
        assert!(deserialized.metadata.dependencies.is_empty());
        assert!(deserialized.metadata.tags.is_empty());
        assert!(deserialized.scripts.is_empty());
    }

    #[test]
    fn test_skill_input_default() {
        let input = SkillInput::default();
        assert!(input.params.is_null() || input.params.as_object().is_none_or(|m| m.is_empty()));
    }
}