skills 0.0.2

Manage agent skills
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
//! Implementation of the `skills pull` command.

use std::{
    fs,
    path::{Path, PathBuf},
    time::{Duration, SystemTime},
};

use inquire::{Confirm, Text, error::InquireError};

use crate::{
    catalog::Catalog,
    commands::{ColorChoice, init},
    config::Config,
    diagnostics::Diagnostics,
    diff::{colorize_diff, unified_diff, write_output},
    error::{Error, Result},
    paths::{display_path, expand_source_path},
    skill::{SKILL_FILE_NAME, SkillTemplate, ToolSkill, render_template},
    status::normalize_line_endings,
    tool::Tool,
};

/// Execute the pull command.
pub async fn run(
    color: ColorChoice,
    verbose: bool,
    skill: Option<String>,
    to: Option<PathBuf>,
) -> Result<()> {
    init::ensure().await?;
    let mut diagnostics = Diagnostics::new(verbose);
    let config = Config::load()?;
    let catalog = Catalog::load(&config, &mut diagnostics);
    let target_override = resolve_target_override(&to)?;

    let mut plans = collect_pull_plans(&catalog, skill.as_deref(), &mut diagnostics)?;
    if plans.is_empty() {
        println!("No modified skills found.");
        return Ok(());
    }

    plans.sort_by(|left, right| left.name.to_lowercase().cmp(&right.name.to_lowercase()));

    print_plan_summary(&plans);

    let use_color = color.enabled();

    for plan in plans.drain(..) {
        match plan.variants.as_slice() {
            [] => continue,
            [variant] => {
                if !confirm_pull(&plan, variant)? {
                    continue;
                }
                let target = select_target_source(&config, &plan, target_override.as_ref())?;
                apply_pull(&plan, variant, &target)?;
                println!(
                    "Pulled {} from {} -> {}",
                    plan.name,
                    variant.tool.display_name(),
                    display_path(&target)
                );
            }
            variants => {
                let selected = resolve_conflict(&plan, variants, use_color)?;
                let Some(selected) = selected else {
                    continue;
                };
                let target = select_target_source(&config, &plan, target_override.as_ref())?;
                apply_pull(&plan, &selected, &target)?;
                println!(
                    "Pulled {} from {} -> {}",
                    plan.name,
                    selected.tool.display_name(),
                    display_path(&target)
                );
            }
        }
    }

    diagnostics.print_skipped_summary();
    diagnostics.print_warning_summary();
    Ok(())
}

/// Pull plan for a single skill.
#[derive(Debug, Clone)]
struct PullPlan {
    /// Skill name.
    name: String,
    /// Source skill if present.
    source: Option<SkillTemplate>,
    /// Tool variants to pull from.
    variants: Vec<PullVariant>,
}

/// Tool-specific variant for pulling.
#[derive(Debug, Clone)]
struct PullVariant {
    /// Tool origin for the variant.
    tool: Tool,
    /// Tool skill contents.
    skill: ToolSkill,
    /// Whether this is an orphaned skill.
    orphan: bool,
}

/// Build pull plans from catalog data.
fn collect_pull_plans(
    catalog: &Catalog,
    skill: Option<&str>,
    diagnostics: &mut Diagnostics,
) -> Result<Vec<PullPlan>> {
    let mut names = Vec::new();
    for name in catalog.sources.keys() {
        names.push(name.clone());
    }
    for tool_map in catalog.tools.values() {
        for name in tool_map.keys() {
            if !names.contains(name) {
                names.push(name.clone());
            }
        }
    }

    if let Some(skill) = skill {
        if !names.iter().any(|name| name == skill) {
            return Err(Error::SkillNotFound {
                name: skill.to_string(),
            });
        }
        names.retain(|name| name == skill);
    }

    let mut plans = Vec::new();
    for name in names {
        let source = catalog.sources.get(&name).cloned();
        let mut variants = Vec::new();

        for tool in Tool::all() {
            let tool_map = catalog.tools.get(&tool);
            let tool_skill = tool_map.and_then(|skills| skills.get(&name));
            let Some(tool_skill) = tool_skill else {
                continue;
            };

            if let Some(source) = &source {
                let rendered = match render_template(&source.contents, tool) {
                    Ok(rendered) => rendered,
                    Err(error) => {
                        diagnostics.warn_skipped(&source.skill_path, error);
                        variants.clear();
                        break;
                    }
                };

                let modified = normalize_line_endings(&rendered)
                    != normalize_line_endings(&tool_skill.contents);
                if modified {
                    variants.push(PullVariant {
                        tool,
                        skill: tool_skill.clone(),
                        orphan: false,
                    });
                }
            } else {
                variants.push(PullVariant {
                    tool,
                    skill: tool_skill.clone(),
                    orphan: true,
                });
            }
        }

        if !variants.is_empty() {
            plans.push(PullPlan {
                name,
                source,
                variants,
            });
        }
    }

    Ok(plans)
}

/// Print a summary of pull candidates.
fn print_plan_summary(plans: &[PullPlan]) {
    println!("Found {} modified skills:\n", plans.len());
    for plan in plans {
        let source_path = plan
            .source
            .as_ref()
            .map(|skill| display_path(&skill.skill_path))
            .unwrap_or_else(|| "-".to_string());
        println!("{}", plan.name);
        println!("  source: {}", source_path);
        for variant in &plan.variants {
            println!(
                "  tool:   {} ({})",
                display_path(&variant.skill.skill_path),
                variant.tool.display_name()
            );
        }
        println!();
    }
}

/// Confirm pulling a single variant.
fn confirm_pull(plan: &PullPlan, variant: &PullVariant) -> Result<bool> {
    let prompt = if variant.orphan {
        format!(
            "Create skill '{}' from {}?",
            plan.name,
            variant.tool.display_name()
        )
    } else {
        format!(
            "Pull changes for '{}' from {}?",
            plan.name,
            variant.tool.display_name()
        )
    };
    confirm(&prompt)
}

/// Resolve conflicts between multiple tool variants.
fn resolve_conflict(
    plan: &PullPlan,
    variants: &[PullVariant],
    color: bool,
) -> Result<Option<PullVariant>> {
    loop {
        println!(
            "{} has different modifications in multiple tools:\n",
            plan.name
        );
        for (index, variant) in variants.iter().enumerate() {
            println!(
                "  [{}] {}  (modified {})",
                index + 1,
                variant.tool.display_name(),
                format_age(variant.skill.modified)
            );
        }
        println!("  [d] Show diff between versions");
        println!("  [s] Skip");

        let choice = Text::new("Which version to pull? [1/2/d/s]")
            .with_default("s")
            .prompt();

        let choice = match choice {
            Ok(value) => value,
            Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
                return Err(Error::PromptCanceled);
            }
            Err(error) => {
                return Err(Error::PromptFailed {
                    message: error.to_string(),
                });
            }
        };

        match choice.as_str() {
            "s" | "S" => return Ok(None),
            "d" | "D" => {
                show_variant_diff(variants, color)?;
                continue;
            }
            _ => {
                if let Ok(index) = choice.parse::<usize>()
                    && let Some(variant) = variants.get(index.saturating_sub(1))
                {
                    return Ok(Some(variant.clone()));
                }
            }
        }
    }
}

/// Show a diff between the first two variants.
fn show_variant_diff(variants: &[PullVariant], color: bool) -> Result<()> {
    if variants.len() < 2 {
        return Ok(());
    }

    let left = &variants[0];
    let right = &variants[1];
    let diff_text = unified_diff(
        &display_path(&left.skill.skill_path),
        &display_path(&right.skill.skill_path),
        &left.skill.contents,
        &right.skill.contents,
    );
    let diff_text = colorize_diff(&diff_text, color);
    write_output(&diff_text, None)?;
    Ok(())
}

/// Select the target source directory for a pull.
fn select_target_source(
    config: &Config,
    plan: &PullPlan,
    override_path: Option<&PathBuf>,
) -> Result<PathBuf> {
    if let Some(source) = &plan.source {
        return Ok(source.source_root.clone());
    }

    if let Some(path) = override_path {
        return Ok(path.clone());
    }

    let sources = config.sources();
    if sources.len() == 1 {
        return Ok(sources[0].clone());
    }

    println!("Available sources:");
    for (index, source) in sources.iter().enumerate() {
        println!("  [{}] {}", index + 1, display_path(source));
    }

    let prompt = "Select target source (press Enter for default)";
    let response = Text::new(prompt).with_default("1").prompt();
    let response = match response {
        Ok(value) => value,
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            return Err(Error::PromptCanceled);
        }
        Err(error) => {
            return Err(Error::PromptFailed {
                message: error.to_string(),
            });
        }
    };

    let trimmed = response.trim();
    if trimmed.is_empty() || trimmed == "1" {
        return Ok(sources[0].clone());
    }

    if let Ok(index) = trimmed.parse::<usize>()
        && let Some(source) = sources.get(index.saturating_sub(1))
    {
        return Ok(source.clone());
    }

    let normalized = expand_source_path(trimmed, Path::new("."))?;
    Ok(normalized)
}

/// Apply a pull variant to the selected source.
fn apply_pull(plan: &PullPlan, variant: &PullVariant, target: &Path) -> Result<()> {
    let skill_dir = if let Some(source) = &plan.source {
        source.skill_dir.clone()
    } else {
        target.join(&plan.name)
    };

    fs::create_dir_all(&skill_dir).map_err(|error| Error::SkillWrite {
        path: skill_dir.clone(),
        source: error,
    })?;

    let skill_path = skill_dir.join(SKILL_FILE_NAME);
    fs::write(&skill_path, &variant.skill.contents).map_err(|error| Error::SkillWrite {
        path: skill_path,
        source: error,
    })?;

    Ok(())
}

/// Resolve the `--to` override path if provided.
fn resolve_target_override(to: &Option<PathBuf>) -> Result<Option<PathBuf>> {
    let Some(path) = to else {
        return Ok(None);
    };

    let normalized = expand_source_path(
        path.to_str()
            .ok_or_else(|| Error::PathNotUnicode { path: path.clone() })?,
        Path::new("."),
    )?;

    if !normalized.is_dir() {
        return Err(Error::PathMissing { path: normalized });
    }

    Ok(Some(normalized))
}

/// Prompt for confirmation.
fn confirm(message: &str) -> Result<bool> {
    match Confirm::new(message).with_default(false).prompt() {
        Ok(value) => Ok(value),
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            Err(Error::PromptCanceled)
        }
        Err(error) => Err(Error::PromptFailed {
            message: error.to_string(),
        }),
    }
}

/// Format a relative age from a timestamp.
fn format_age(modified: SystemTime) -> String {
    let elapsed = modified.elapsed().unwrap_or(Duration::from_secs(0));
    let seconds = elapsed.as_secs();
    if seconds < 60 {
        return "moments ago".to_string();
    }
    if seconds < 60 * 60 {
        let minutes = seconds / 60;
        return format!("{} minute{} ago", minutes, plural(minutes));
    }
    if seconds < 60 * 60 * 24 {
        let hours = seconds / 60 / 60;
        return format!("{} hour{} ago", hours, plural(hours));
    }
    let days = seconds / 60 / 60 / 24;
    format!("{days} day{} ago", plural(days))
}

/// Return a plural suffix for counts.
fn plural(count: u64) -> &'static str {
    if count == 1 { "" } else { "s" }
}