localgpt 0.1.3

A local device focused AI assistant with persistent markdown memory, autonomous heartbeat tasks, and semantic search. Single binary, no runtime dependencies.
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Skills system for LocalGPT (OpenClaw-compatible)
//!
//! Skills are SKILL.md files that provide specialized instructions for specific tasks.
//! Supports multiple sources, requirements gating, and slash command invocation.

use anyhow::Result;
use serde::Deserialize;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::{debug, warn};

/// Skill requirements for eligibility gating
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct SkillRequirements {
    /// Required binaries (all must exist on PATH)
    #[serde(default)]
    pub bins: Vec<String>,

    /// At least one of these binaries must exist
    #[serde(default, rename = "anyBins")]
    pub any_bins: Vec<String>,

    /// Required environment variables
    #[serde(default)]
    pub env: Vec<String>,
}

/// OpenClaw metadata in frontmatter
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct SkillMetadata {
    /// Emoji for display
    pub emoji: Option<String>,

    /// Skip eligibility checks if true
    #[serde(default)]
    pub always: bool,

    /// Requirements for this skill
    #[serde(default)]
    pub requires: SkillRequirements,
}

/// Frontmatter parsed from SKILL.md
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct SkillFrontmatter {
    /// Skill name (overrides directory name)
    pub name: Option<String>,

    /// Brief description
    pub description: Option<String>,

    /// Whether this skill can be invoked via slash command (default: true)
    #[serde(default = "default_true", rename = "user-invocable")]
    pub user_invocable: bool,

    /// Whether to exclude from model's system prompt (default: false)
    #[serde(default, rename = "disable-model-invocation")]
    pub disable_model_invocation: bool,

    /// Dispatch slash command directly to a tool
    #[serde(rename = "command-dispatch")]
    pub command_dispatch: Option<String>,

    /// Tool name for command dispatch
    #[serde(rename = "command-tool")]
    pub command_tool: Option<String>,

    /// OpenClaw-specific metadata
    #[serde(default)]
    pub metadata: Option<SkillMetadataWrapper>,
}

/// Wrapper for nested metadata (handles both flat and nested openclaw key)
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct SkillMetadataWrapper {
    #[serde(default)]
    pub openclaw: Option<SkillMetadata>,

    // Flat fields (fallback)
    pub emoji: Option<String>,
    #[serde(default)]
    pub requires: Option<SkillRequirements>,
}

fn default_true() -> bool {
    true
}

/// Source of a skill
#[derive(Debug, Clone, PartialEq)]
pub enum SkillSource {
    /// Workspace-level skill (highest priority)
    Workspace,
    /// User-managed skill (~/.localgpt/skills/)
    Managed,
    /// Bundled with the application
    Bundled,
}

/// Eligibility status for a skill
#[derive(Debug, Clone)]
pub enum SkillEligibility {
    /// Skill is ready to use
    Ready,
    /// Missing required binaries
    MissingBins(Vec<String>),
    /// Missing required environment variables
    MissingEnv(Vec<String>),
    /// Missing at least one of anyBins
    MissingAnyBins(Vec<String>),
}

impl SkillEligibility {
    pub fn is_ready(&self) -> bool {
        matches!(self, SkillEligibility::Ready)
    }
}

/// A skill loaded from SKILL.md
#[derive(Debug, Clone)]
pub struct Skill {
    /// Skill name (from frontmatter or directory name)
    pub name: String,

    /// Sanitized command name for slash commands
    pub command_name: String,

    /// Path to SKILL.md
    pub path: PathBuf,

    /// Brief description
    pub description: String,

    /// Emoji for display
    pub emoji: Option<String>,

    /// Source of the skill
    pub source: SkillSource,

    /// Whether this skill can be invoked via slash command
    pub user_invocable: bool,

    /// Whether to exclude from model's system prompt
    pub disable_model_invocation: bool,

    /// Direct tool dispatch configuration
    pub command_dispatch: Option<CommandDispatch>,

    /// Requirements for eligibility
    pub requires: SkillRequirements,

    /// Current eligibility status
    pub eligibility: SkillEligibility,
}

/// Command dispatch configuration for direct tool execution
#[derive(Debug, Clone)]
pub struct CommandDispatch {
    /// Dispatch type (currently only "tool")
    pub kind: String,
    /// Tool name to dispatch to
    pub tool_name: String,
}

impl Skill {
    /// Check if this skill should be included in the model's system prompt
    pub fn include_in_prompt(&self) -> bool {
        !self.disable_model_invocation && self.eligibility.is_ready()
    }

    /// Check if this skill can be invoked via slash command
    pub fn can_invoke(&self) -> bool {
        self.user_invocable && self.eligibility.is_ready()
    }
}

/// Load all skills from multiple sources
/// Returns skills sorted by name with workspace skills taking priority over managed
pub fn load_skills(workspace: &Path) -> Result<Vec<Skill>> {
    let mut skills_map: HashMap<String, Skill> = HashMap::new();

    // Load from managed directory first (lower priority)
    if let Some(managed_dir) = get_managed_skills_dir()
        && managed_dir.exists()
    {
        for skill in load_skills_from_dir(&managed_dir, SkillSource::Managed)? {
            skills_map.insert(skill.name.clone(), skill);
        }
    }

    // Load from workspace (higher priority, overwrites managed)
    let workspace_skills_dir = workspace.join("skills");
    if workspace_skills_dir.exists() {
        for skill in load_skills_from_dir(&workspace_skills_dir, SkillSource::Workspace)? {
            skills_map.insert(skill.name.clone(), skill);
        }
    }

    // Convert to vec and sort
    let mut skills: Vec<Skill> = skills_map.into_values().collect();
    skills.sort_by(|a, b| a.name.cmp(&b.name));

    debug!("Loaded {} skills", skills.len());
    Ok(skills)
}

/// Get the managed skills directory (~/.localgpt/skills/)
fn get_managed_skills_dir() -> Option<PathBuf> {
    directories::BaseDirs::new().map(|dirs| dirs.home_dir().join(".localgpt").join("skills"))
}

/// Load skills from a single directory
fn load_skills_from_dir(dir: &Path, source: SkillSource) -> Result<Vec<Skill>> {
    let mut skills = Vec::new();

    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if !path.is_dir() {
            continue;
        }

        let skill_file = path.join("SKILL.md");
        if !skill_file.exists() {
            continue;
        }

        let dir_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string();

        match load_skill(&skill_file, &dir_name, source.clone()) {
            Ok(skill) => skills.push(skill),
            Err(e) => {
                warn!("Failed to load skill from {:?}: {}", skill_file, e);
            }
        }
    }

    Ok(skills)
}

/// Load a single skill from a SKILL.md file
fn load_skill(path: &Path, dir_name: &str, source: SkillSource) -> Result<Skill> {
    let content = fs::read_to_string(path)?;
    let (frontmatter, body) = parse_frontmatter(&content);

    // Get name from frontmatter or directory
    let name = frontmatter
        .name
        .clone()
        .unwrap_or_else(|| dir_name.to_string());

    // Generate sanitized command name
    let command_name = sanitize_command_name(&name);

    // Get description from frontmatter or body
    let description = frontmatter
        .description
        .clone()
        .unwrap_or_else(|| extract_description_from_body(&body));

    // Extract metadata
    let (emoji, requires, always) = if let Some(ref meta) = frontmatter.metadata {
        if let Some(ref oc) = meta.openclaw {
            (oc.emoji.clone(), oc.requires.clone(), oc.always)
        } else {
            (
                meta.emoji.clone(),
                meta.requires.clone().unwrap_or_default(),
                false,
            )
        }
    } else {
        (None, SkillRequirements::default(), false)
    };

    // Check eligibility (skip if always=true)
    let eligibility = if always {
        SkillEligibility::Ready
    } else {
        check_eligibility(&requires)
    };

    // Parse command dispatch
    let command_dispatch = if frontmatter.command_dispatch.as_deref() == Some("tool") {
        frontmatter.command_tool.map(|tool_name| CommandDispatch {
            kind: "tool".to_string(),
            tool_name,
        })
    } else {
        None
    };

    Ok(Skill {
        name,
        command_name,
        path: path.to_path_buf(),
        description,
        emoji,
        source,
        user_invocable: frontmatter.user_invocable,
        disable_model_invocation: frontmatter.disable_model_invocation,
        command_dispatch,
        requires,
        eligibility,
    })
}

/// Parse YAML frontmatter from content
fn parse_frontmatter(content: &str) -> (SkillFrontmatter, String) {
    let lines: Vec<&str> = content.lines().collect();

    // Check for frontmatter
    if lines.first().map(|l| l.trim()) != Some("---") {
        return (SkillFrontmatter::default(), content.to_string());
    }

    // Find closing ---
    let end_idx = lines
        .iter()
        .skip(1)
        .position(|l| l.trim() == "---")
        .map(|i| i + 1);

    let Some(end_idx) = end_idx else {
        return (SkillFrontmatter::default(), content.to_string());
    };

    // Extract frontmatter YAML
    let yaml_content: String = lines[1..end_idx].join("\n");
    let body: String = lines[end_idx + 1..].join("\n");

    // Parse YAML
    match serde_yaml::from_str(&yaml_content) {
        Ok(fm) => (fm, body),
        Err(e) => {
            debug!("Failed to parse frontmatter: {}", e);
            (SkillFrontmatter::default(), content.to_string())
        }
    }
}

/// Extract description from markdown body
fn extract_description_from_body(body: &str) -> String {
    for line in body.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        return trimmed.chars().take(100).collect();
    }
    String::new()
}

/// Sanitize skill name to command name (lowercase, special chars to hyphens)
fn sanitize_command_name(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect::<String>()
        .trim_matches('-')
        .to_string()
        .chars()
        .take(32)
        .collect()
}

/// Check if a skill meets its requirements
fn check_eligibility(requires: &SkillRequirements) -> SkillEligibility {
    // Check required binaries
    let missing_bins: Vec<String> = requires
        .bins
        .iter()
        .filter(|bin| !has_binary(bin))
        .cloned()
        .collect();

    if !missing_bins.is_empty() {
        return SkillEligibility::MissingBins(missing_bins);
    }

    // Check anyBins (at least one must exist)
    if !requires.any_bins.is_empty() {
        let has_any = requires.any_bins.iter().any(|bin| has_binary(bin));
        if !has_any {
            return SkillEligibility::MissingAnyBins(requires.any_bins.clone());
        }
    }

    // Check environment variables
    let missing_env: Vec<String> = requires
        .env
        .iter()
        .filter(|var| env::var(var).is_err())
        .cloned()
        .collect();

    if !missing_env.is_empty() {
        return SkillEligibility::MissingEnv(missing_env);
    }

    SkillEligibility::Ready
}

/// Check if a binary exists on PATH
fn has_binary(name: &str) -> bool {
    Command::new("which")
        .arg(name)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Result of parsing a slash command
#[derive(Debug, Clone)]
pub struct SkillInvocation {
    /// The skill being invoked
    pub skill_name: String,
    /// Arguments passed to the skill
    pub args: String,
    /// Direct dispatch configuration (if skill has it)
    pub dispatch: Option<CommandDispatch>,
}

/// Parse a slash command and find matching skill
/// Returns None if not a skill command or skill not found
pub fn parse_skill_command(input: &str, skills: &[Skill]) -> Option<SkillInvocation> {
    let input = input.trim();

    // Must start with /
    if !input.starts_with('/') {
        return None;
    }

    // Extract command and args
    let without_slash = &input[1..];
    let (cmd, args) = match without_slash.split_once(char::is_whitespace) {
        Some((c, a)) => (c.trim(), a.trim().to_string()),
        None => (without_slash.trim(), String::new()),
    };

    // Normalize command (lowercase, hyphens)
    let normalized_cmd = cmd.to_lowercase().replace('_', "-");

    // Find matching skill
    for skill in skills {
        if !skill.can_invoke() {
            continue;
        }

        // Match by command_name or name
        let skill_cmd = skill.command_name.replace('_', "-");
        let skill_name_normalized = skill.name.to_lowercase().replace('_', "-");

        if normalized_cmd == skill_cmd || normalized_cmd == skill_name_normalized {
            return Some(SkillInvocation {
                skill_name: skill.name.clone(),
                args,
                dispatch: skill.command_dispatch.clone(),
            });
        }
    }

    None
}

/// Build skills prompt section for the system prompt
pub fn build_skills_prompt(skills: &[Skill]) -> String {
    // Filter to skills that should be in the prompt
    let prompt_skills: Vec<&Skill> = skills.iter().filter(|s| s.include_in_prompt()).collect();

    if prompt_skills.is_empty() {
        return String::new();
    }

    let mut lines = vec![
        "## Skills".to_string(),
        String::new(),
        "Before replying: scan available skills below. If one clearly applies, \
         read its SKILL.md with read_file, then follow it."
            .to_string(),
        String::new(),
        "<available_skills>".to_string(),
    ];

    for skill in &prompt_skills {
        let emoji_prefix = skill
            .emoji
            .as_ref()
            .map(|e| format!("{} ", e))
            .unwrap_or_default();

        let command_info = if skill.user_invocable {
            format!(" (or use /{} command)", skill.command_name)
        } else {
            String::new()
        };

        lines.push(format!(
            "- {}{}: {}{}",
            emoji_prefix, skill.name, skill.description, command_info
        ));
        lines.push(format!("  location: {}", skill.path.display()));
    }

    lines.push("</available_skills>".to_string());
    lines.push(String::new());

    // List user-invocable skills
    let invocable: Vec<&Skill> = skills.iter().filter(|s| s.can_invoke()).collect();
    if !invocable.is_empty() {
        lines.push("Available slash commands:".to_string());
        for skill in &invocable {
            let emoji = skill
                .emoji
                .as_ref()
                .map(|e| format!(" {}", e))
                .unwrap_or_default();
            lines.push(format!(
                "- /{}{} - {}",
                skill.command_name, emoji, skill.description
            ));
        }
        lines.push(String::new());
    }

    lines.push("Rules:".to_string());
    lines.push(
        "- If exactly one skill clearly applies: read its SKILL.md, then follow it.".to_string(),
    );
    lines.push("- If multiple could apply: choose the most specific one.".to_string());
    lines.push("- If none clearly apply: do not read any SKILL.md.".to_string());
    lines.push(String::new());

    lines.join("\n")
}

/// Get skill status summary for CLI display
pub fn get_skills_summary(skills: &[Skill]) -> String {
    let ready: Vec<&Skill> = skills.iter().filter(|s| s.eligibility.is_ready()).collect();
    let blocked: Vec<&Skill> = skills
        .iter()
        .filter(|s| !s.eligibility.is_ready())
        .collect();

    let mut lines = Vec::new();
    lines.push(format!(
        "Skills: {} ready, {} blocked",
        ready.len(),
        blocked.len()
    ));

    if !ready.is_empty() {
        lines.push(String::new());
        lines.push("Ready:".to_string());
        for skill in &ready {
            let emoji = skill
                .emoji
                .as_ref()
                .map(|e| format!(" {}", e))
                .unwrap_or_default();
            let source = match skill.source {
                SkillSource::Workspace => "[workspace]",
                SkillSource::Managed => "[managed]",
                SkillSource::Bundled => "[bundled]",
            };
            lines.push(format!(
                "  /{}{} - {} {}",
                skill.command_name, emoji, skill.description, source
            ));
        }
    }

    if !blocked.is_empty() {
        lines.push(String::new());
        lines.push("Blocked:".to_string());
        for skill in &blocked {
            let reason = match &skill.eligibility {
                SkillEligibility::Ready => "ready".to_string(),
                SkillEligibility::MissingBins(bins) => format!("missing bins: {}", bins.join(", ")),
                SkillEligibility::MissingEnv(vars) => format!("missing env: {}", vars.join(", ")),
                SkillEligibility::MissingAnyBins(bins) => {
                    format!("need one of: {}", bins.join(", "))
                }
            };
            lines.push(format!("  {} - {}", skill.name, reason));
        }
    }

    lines.join("\n")
}

/// Extract description from skill content (used by tests)
#[allow(dead_code)]
fn extract_description(content: &str) -> String {
    let (fm, body) = parse_frontmatter(content);
    fm.description
        .unwrap_or_else(|| extract_description_from_body(&body))
}

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

    #[test]
    fn test_parse_frontmatter() {
        let content = r#"---
name: test-skill
description: "A test skill"
user-invocable: true
disable-model-invocation: false
---
# Test Skill

This is the body.
"#;
        let (fm, body) = parse_frontmatter(content);
        assert_eq!(fm.name, Some("test-skill".to_string()));
        assert_eq!(fm.description, Some("A test skill".to_string()));
        assert!(fm.user_invocable);
        assert!(!fm.disable_model_invocation);
        assert!(body.contains("This is the body"));
    }

    #[test]
    fn test_parse_frontmatter_with_metadata() {
        let content = r#"---
name: github-pr
description: "Create GitHub PRs"
metadata:
  openclaw:
    emoji: "🐙"
    requires:
      bins: ["gh", "git"]
      env: ["GITHUB_TOKEN"]
---
"#;
        let (fm, _) = parse_frontmatter(content);
        assert_eq!(fm.name, Some("github-pr".to_string()));

        let meta = fm.metadata.unwrap();
        let oc = meta.openclaw.unwrap();
        assert_eq!(oc.emoji, Some("🐙".to_string()));
        assert_eq!(oc.requires.bins, vec!["gh", "git"]);
        assert_eq!(oc.requires.env, vec!["GITHUB_TOKEN"]);
    }

    #[test]
    fn test_sanitize_command_name() {
        assert_eq!(sanitize_command_name("GitHub PR"), "github-pr");
        assert_eq!(sanitize_command_name("test_skill"), "test-skill");
        assert_eq!(sanitize_command_name("My Cool Skill!"), "my-cool-skill");
    }

    #[test]
    fn test_extract_description() {
        let content = r#"---
name: test
---
# Test Skill

This is a test skill that does something useful.
"#;
        let desc = extract_description(content);
        assert_eq!(desc, "This is a test skill that does something useful.");
    }

    #[test]
    fn test_extract_description_no_frontmatter() {
        let content = r#"# My Skill

A skill for doing things.
"#;
        let desc = extract_description(content);
        assert_eq!(desc, "A skill for doing things.");
    }

    #[test]
    fn test_parse_skill_command() {
        let skills = vec![Skill {
            name: "github-pr".to_string(),
            command_name: "github-pr".to_string(),
            path: PathBuf::from("/test/SKILL.md"),
            description: "Create PRs".to_string(),
            emoji: Some("🐙".to_string()),
            source: SkillSource::Workspace,
            user_invocable: true,
            disable_model_invocation: false,
            command_dispatch: None,
            requires: SkillRequirements::default(),
            eligibility: SkillEligibility::Ready,
        }];

        // Match by command name
        let result = parse_skill_command("/github-pr create feature", &skills);
        assert!(result.is_some());
        let inv = result.unwrap();
        assert_eq!(inv.skill_name, "github-pr");
        assert_eq!(inv.args, "create feature");

        // No match
        let result = parse_skill_command("/unknown-skill", &skills);
        assert!(result.is_none());

        // Not a command
        let result = parse_skill_command("hello", &skills);
        assert!(result.is_none());
    }
}