lingshu-tools 0.10.0

Tool registry, ToolHandler trait, and 50+ tool implementations
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
use std::path::Path;

use lingshu_types::ToolError;
use regex::Regex;
use serde_json::Value;

use super::pending_store::{
    SUBSYSTEM_SKILLS, PendingWriteRecord, discard_pending, get_pending, list_pending, stage_write,
};
use super::usage::{bump_patch, format_usage_summary, is_pinned, set_pinned};

pub fn skills_write_approval_enabled(config_enabled: bool) -> bool {
    config_enabled
}

fn stage_skill_write(
    home: &Path,
    payload: Value,
    summary: &str,
    origin: &str,
) -> PendingWriteRecord {
    let action = payload
        .get("action")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();
    stage_write(home, SUBSYSTEM_SKILLS, payload, summary, origin, &action)
}

use std::sync::LazyLock;

static FM_DESC: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?m)^description:\s*(.+)$").expect("regex"));

pub fn skill_gist(payload: &Value) -> String {
    let action = payload.get("action").and_then(|v| v.as_str()).unwrap_or("");
    let name = payload.get("name").and_then(|v| v.as_str()).unwrap_or("");
    match action {
        "create" | "edit" => {
            let content = payload
                .get("content")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            let size = if content.len() >= 1024 {
                format!("{} KB", content.len() / 1024 + 1)
            } else {
                format!("{} chars", content.len())
            };
            let verb = if action == "create" {
                "create"
            } else {
                "rewrite"
            };
            if let Some(desc) = frontmatter_description(content) {
                format!("{verb} '{name}' — {desc} ({size})")
            } else {
                format!("{verb} '{name}' ({size})")
            }
        }
        "patch" => {
            let target = payload
                .get("file_path")
                .and_then(|v| v.as_str())
                .unwrap_or("SKILL.md");
            format!("patch '{name}' {target}")
        }
        "write_file" => {
            let fp = payload
                .get("file_path")
                .and_then(|v| v.as_str())
                .unwrap_or("?");
            format!("write {fp} in '{name}'")
        }
        "remove_file" => {
            let fp = payload
                .get("file_path")
                .and_then(|v| v.as_str())
                .unwrap_or("?");
            format!("remove {fp} from '{name}'")
        }
        "delete" => format!("delete skill '{name}'"),
        other => format!("{other} '{name}'"),
    }
}

fn frontmatter_description(content: &str) -> Option<String> {
    FM_DESC.captures(content).and_then(|c| c.get(1)).map(|m| {
        m.as_str()
            .trim()
            .trim_matches(['\'', '"'])
            .chars()
            .take(140)
            .collect()
    })
}

pub enum SkillManageGate {
    Allow,
    Staged(String),
}

pub fn maybe_gate_skill_manage(
    home: &Path,
    payload: Value,
    write_approval: bool,
) -> SkillManageGate {
    if !write_approval {
        return SkillManageGate::Allow;
    }
    let gist = skill_gist(&payload);
    let record = stage_skill_write(home, payload, &gist, "foreground");
    SkillManageGate::Staged(format!(
        "Staged for approval (skills.write_approval is on). Not yet saved — review with /skills pending.\n\
         Pending id: {}\n{}",
        record.id, gist
    ))
}

pub async fn apply_skill_manage_payload(home: &Path, payload: &Value) -> Result<String, ToolError> {
    let action = payload
        .get("action")
        .and_then(|v| v.as_str())
        .ok_or_else(|| ToolError::InvalidArgs {
            tool: "skill_manage".into(),
            message: "missing action".into(),
        })?;
    let name = payload
        .get("name")
        .and_then(|v| v.as_str())
        .ok_or_else(|| ToolError::InvalidArgs {
            tool: "skill_manage".into(),
            message: "missing name".into(),
        })?;
    let skills_base = home.join("skills");
    let category = payload.get("category").and_then(|v| v.as_str());
    let skill_dir = if action == "create" {
        if let Some(cat) = category {
            skills_base.join(cat).join(name)
        } else {
            skills_base.join(name)
        }
    } else {
        crate::tools::skills::find_skill_dir_public(&skills_base, name)
            .unwrap_or_else(|| skills_base.join(name))
    };

    match action {
        "create" | "edit" => {
            let content = payload
                .get("content")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "content required".into(),
                })?;
            tokio::fs::create_dir_all(&skill_dir)
                .await
                .map_err(|e| ToolError::Other(format!("Cannot create skill dir: {e}")))?;
            let skill_path = skill_dir.join("SKILL.md");
            tokio::fs::write(&skill_path, content)
                .await
                .map_err(|e| ToolError::Other(format!("Cannot write SKILL.md: {e}")))?;
            if action == "edit" {
                bump_patch(home, name);
            }
            Ok(format!(
                "Skill '{name}' {action}d at {}",
                skill_path.display()
            ))
        }
        "delete" => {
            if !skill_dir.exists() {
                return Err(ToolError::NotFound(format!(
                    "Skill '{name}' does not exist"
                )));
            }
            tokio::fs::remove_dir_all(&skill_dir)
                .await
                .map_err(|e| ToolError::Other(format!("Cannot delete skill: {e}")))?;
            Ok(format!("Skill '{name}' deleted."))
        }
        "patch" => {
            let old = payload
                .get("old_string")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "old_string required".into(),
                })?;
            let new = payload
                .get("new_string")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "new_string required".into(),
                })?;
            let replace_all = payload
                .get("replace_all")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let path = skill_dir.join("SKILL.md");
            if !path.is_file() {
                return Err(ToolError::NotFound(format!(
                    "Skill '{name}' does not exist"
                )));
            }
            let current = tokio::fs::read_to_string(&path)
                .await
                .map_err(|e| ToolError::Other(format!("Cannot read SKILL.md: {e}")))?;
            let count = current.matches(old).count();
            if count == 0 {
                return Err(ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: format!(
                        "old_string not found in skill '{name}'. Verify the exact text including whitespace."
                    ),
                });
            }
            if count > 1 && !replace_all {
                return Err(ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: format!(
                        "old_string matches {count} times in skill '{name}'. Use replace_all=true to replace all, or provide more context."
                    ),
                });
            }
            let updated = if replace_all {
                current.replace(old, new)
            } else {
                current.replacen(old, new, 1)
            };
            tokio::fs::write(&path, updated)
                .await
                .map_err(|e| ToolError::Other(e.to_string()))?;
            bump_patch(home, name);
            let replaced = if replace_all { count } else { 1 };
            Ok(format!(
                "Skill '{name}' patched: replaced {replaced} occurrence(s)."
            ))
        }
        "write_file" => {
            let fp = payload
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "file_path required".into(),
                })?;
            let fc = payload
                .get("file_content")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "file_content required".into(),
                })?;
            validate_supporting_path(fp)?;
            if !skill_dir.exists() {
                return Err(ToolError::NotFound(format!(
                    "Skill '{name}' does not exist. Create it first."
                )));
            }
            let file_path = skill_dir.join(fp);
            if let Some(parent) = file_path.parent() {
                tokio::fs::create_dir_all(parent)
                    .await
                    .map_err(|e| ToolError::Other(e.to_string()))?;
            }
            tokio::fs::write(&file_path, fc)
                .await
                .map_err(|e| ToolError::Other(e.to_string()))?;
            Ok(format!("Wrote '{fp}' in skill '{name}'."))
        }
        "remove_file" => {
            let fp = payload
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidArgs {
                    tool: "skill_manage".into(),
                    message: "file_path required".into(),
                })?;
            validate_supporting_path(fp)?;
            let file_path = skill_dir.join(fp);
            if !file_path.is_file() {
                return Err(ToolError::NotFound(format!(
                    "File '{fp}' not found in skill '{name}'"
                )));
            }
            tokio::fs::remove_file(&file_path)
                .await
                .map_err(|e| ToolError::Other(e.to_string()))?;
            Ok(format!("Removed '{fp}' from skill '{name}'."))
        }
        other => Err(ToolError::InvalidArgs {
            tool: "skill_manage".into(),
            message: format!("unknown action '{other}'"),
        }),
    }
}

fn validate_supporting_path(fp: &str) -> Result<(), ToolError> {
    if fp.contains("..") {
        return Err(ToolError::PermissionDenied(
            "Path traversal not allowed".into(),
        ));
    }
    let allowed = ["references/", "templates/", "scripts/", "assets/"];
    if !allowed.iter().any(|p| fp.starts_with(p)) {
        return Err(ToolError::PermissionDenied(format!(
            "file_path must be under references/, templates/, scripts/, or assets/ (got {fp})"
        )));
    }
    Ok(())
}

pub async fn apply_pending_skill_write(home: &Path, id: &str) -> Result<String, ToolError> {
    let record = get_pending(home, SUBSYSTEM_SKILLS, id)
        .ok_or_else(|| ToolError::NotFound(format!("Pending {id} not found")))?;
    let result = apply_skill_manage_payload(home, &record.payload).await?;
    let _ = discard_pending(home, SUBSYSTEM_SKILLS, id);
    Ok(result)
}

pub fn format_skills_pending_state(
    home: &Path,
    write_approval: bool,
    inline_shell: bool,
) -> String {
    format!(
        "skills.write_approval = {}\nskills.inline_shell = {}\n\n{}",
        if write_approval { "on" } else { "off" },
        if inline_shell { "on" } else { "off" },
        format_skills_pending_list(home)
    )
}

fn format_skills_pending_list(home: &Path) -> String {
    let records = list_pending(home, SUBSYSTEM_SKILLS);
    if records.is_empty() {
        return "No pending skills writes.".into();
    }
    let mut lines = vec![format!("Pending skills writes ({}):", records.len())];
    for r in records {
        lines.push(format!("  {}  {}", r.id, r.summary));
    }
    lines.push(String::new());
    lines.push("Apply: /skills approve <id>   Reject: /skills reject <id>".into());
    lines.push("Review diff: /skills diff <id>".into());
    lines.join("\n")
}

pub fn skill_pending_diff(home: &Path, id: &str) -> Option<String> {
    let record = get_pending(home, SUBSYSTEM_SKILLS, id)?;
    let payload = &record.payload;
    let action = payload.get("action")?.as_str()?;
    let name = payload.get("name")?.as_str()?;
    let skills_base = home.join("skills");
    let skill_dir = crate::tools::skills::find_skill_dir_public(&skills_base, name)
        .unwrap_or_else(|| skills_base.join(name));

    match action {
        "create" => payload
            .get("content")
            .and_then(|v| v.as_str())
            .map(str::to_string),
        "edit" => {
            let new = payload.get("content")?.as_str()?;
            let current = std::fs::read_to_string(skill_dir.join("SKILL.md")).unwrap_or_default();
            Some(unified_diff("SKILL.md", &current, new))
        }
        "patch" => {
            let old = payload.get("old_string")?.as_str()?;
            let new = payload.get("new_string")?.as_str()?;
            let current = std::fs::read_to_string(skill_dir.join("SKILL.md")).unwrap_or_default();
            let updated = if payload
                .get("replace_all")
                .and_then(|v| v.as_bool())
                .unwrap_or(false)
            {
                current.replace(old, new)
            } else {
                current.replacen(old, new, 1).to_string()
            };
            Some(unified_diff("SKILL.md", &current, &updated))
        }
        _ => Some(format!(
            "Action: {action}\n{}",
            serde_json::to_string_pretty(payload).unwrap_or_default()
        )),
    }
}

fn unified_diff(path: &str, old: &str, new: &str) -> String {
    if old == new {
        return format!("(no changes for {path})");
    }
    format!("--- {path} (current)\n+++ {path} (pending)\n\n[current]\n{old}\n\n[pending]\n{new}")
}

/// Slash handler context for `/skills` governance subcommands (approval, inline shell).
pub struct SkillsSubcommandContext<'a> {
    pub write_approval: bool,
    pub inline_shell: bool,
    pub set_write_approval: Option<&'a dyn Fn(bool) -> Result<(), String>>,
    pub set_inline_shell: Option<&'a dyn Fn(bool) -> Result<(), String>>,
}

/// Handle `/skills pending|approve|reject|approval|inline-shell|diff|usage` — returns `None` if not matched.
pub fn handle_skills_pending_subcommand(
    home: &Path,
    args: &str,
    ctx: &SkillsSubcommandContext<'_>,
) -> Option<String> {
    let tokens: Vec<&str> = args.split_whitespace().collect();
    let first = tokens.first()?.to_ascii_lowercase();
    match first.as_str() {
        "pending" => Some(format_skills_pending_list(home)),
        "approve" | "apply" => Some(approve_pending(home, tokens.get(1).copied())),
        "reject" | "deny" | "drop" => Some(reject_pending(home, tokens.get(1).copied())),
        "diff" => tokens
            .get(1)
            .and_then(|id| skill_pending_diff(home, id))
            .or_else(|| Some("Usage: /skills diff <id>".into())),
        "approval" | "mode" => Some(set_approval_mode(
            tokens.get(1).copied(),
            ctx.write_approval,
            ctx.set_write_approval,
        )),
        "inline-shell" | "inline_shell" | "shell" => Some(set_inline_shell_mode(
            tokens.get(1).copied(),
            ctx.inline_shell,
            ctx.set_inline_shell,
        )),
        "usage" | "stats" => Some(format_usage_summary(home, 15)),
        "pin" => tokens.get(1).map(|name| {
            if set_pinned(home, name, true) {
                format!("Pinned skill '{name}' (excluded from /curator stale).")
            } else {
                "Usage: /skills pin <skill-name>".into()
            }
        }),
        "unpin" => tokens.get(1).map(|name| {
            if is_pinned(home, name) && set_pinned(home, name, false) {
                format!("Unpinned skill '{name}'.")
            } else {
                format!("Skill '{name}' is not pinned.")
            }
        }),
        _ => None,
    }
}

fn approve_pending(home: &Path, target: Option<&str>) -> String {
    let Some(target) = target else {
        return "Usage: /skills approve <id|all>".into();
    };
    if target.eq_ignore_ascii_case("all") {
        return approve_all_pending(home);
    }
    match tokio::runtime::Handle::try_current() {
        Ok(handle) => match handle.block_on(apply_pending_skill_write(home, target)) {
            Ok(msg) => msg,
            Err(e) => format!("Approve failed: {e}"),
        },
        Err(_) => format!("Cannot approve inline (no async runtime). Pending id: {target}"),
    }
}

fn approve_all_pending(home: &Path) -> String {
    let records = list_pending(home, SUBSYSTEM_SKILLS);
    if records.is_empty() {
        return "No pending skills writes.".into();
    }
    let handle = match tokio::runtime::Handle::try_current() {
        Ok(h) => h,
        Err(_) => return "Cannot approve inline (no async runtime).".into(),
    };
    let mut ok = 0u32;
    let mut errors = Vec::new();
    for record in records {
        match handle.block_on(apply_pending_skill_write(home, &record.id)) {
            Ok(_) => ok += 1,
            Err(e) => errors.push(format!("{}: {e}", record.id)),
        }
    }
    if errors.is_empty() {
        format!("Approved {ok} pending skill write(s).")
    } else {
        format!(
            "Approved {ok}; {} failed:\n{}",
            errors.len(),
            errors.join("\n")
        )
    }
}

fn reject_pending(home: &Path, target: Option<&str>) -> String {
    let Some(target) = target else {
        return "Usage: /skills reject <id|all>".into();
    };
    if target.eq_ignore_ascii_case("all") {
        let ids: Vec<String> = list_pending(home, SUBSYSTEM_SKILLS)
            .into_iter()
            .map(|r| r.id)
            .collect();
        if ids.is_empty() {
            return "No pending skills writes.".into();
        }
        let n = ids.len();
        for id in ids {
            let _ = discard_pending(home, SUBSYSTEM_SKILLS, &id);
        }
        return format!("Rejected {n} pending skill write(s).");
    }
    if discard_pending(home, SUBSYSTEM_SKILLS, target) {
        format!("Rejected pending write {target}.")
    } else {
        format!("No pending write with id {target}.")
    }
}

fn set_approval_mode(
    mode: Option<&str>,
    current: bool,
    set_write_approval: Option<&dyn Fn(bool) -> Result<(), String>>,
) -> String {
    let Some(mode) = mode.map(str::trim).filter(|m| !m.is_empty()) else {
        return format!(
            "skills.write_approval is {}.\nUsage: /skills approval on|off",
            if current { "on" } else { "off" }
        );
    };
    let enabled = match mode.to_ascii_lowercase().as_str() {
        "on" | "true" | "yes" | "1" | "enable" | "enabled" => true,
        "off" | "false" | "no" | "0" | "disable" | "disabled" => false,
        other => return format!("Unknown mode '{other}'. Use on|off."),
    };
    if let Some(set) = set_write_approval {
        match set(enabled) {
            Ok(()) => {
                return format!(
                    "skills.write_approval set to '{}'.",
                    if enabled { "on" } else { "off" }
                );
            }
            Err(e) => return format!("Failed to set skills.write_approval: {e}"),
        }
    }
    format!(
        "Set skills.write_approval: {} in ~/.lingshu/config.yaml (or use /config).",
        enabled
    )
}

fn set_inline_shell_mode(
    mode: Option<&str>,
    current: bool,
    set_inline_shell: Option<&dyn Fn(bool) -> Result<(), String>>,
) -> String {
    let Some(mode) = mode.map(str::trim).filter(|m| !m.is_empty()) else {
        return format!(
            "skills.inline_shell is {}.\nUsage: /skills inline-shell on|off",
            if current { "on" } else { "off" }
        );
    };
    let enabled = match mode.to_ascii_lowercase().as_str() {
        "on" | "true" | "yes" | "1" | "enable" | "enabled" => true,
        "off" | "false" | "no" | "0" | "disable" | "disabled" => false,
        other => return format!("Unknown mode '{other}'. Use on|off."),
    };
    if let Some(set) = set_inline_shell {
        match set(enabled) {
            Ok(()) => {
                return format!(
                    "skills.inline_shell set to '{}'.",
                    if enabled { "on" } else { "off" }
                );
            }
            Err(e) => return format!("Failed to set skills.inline_shell: {e}"),
        }
    }
    format!(
        "Set skills.inline_shell: {} in ~/.lingshu/config.yaml (or use /config).",
        enabled
    )
}


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

    #[test]
    fn stages_and_lists_pending() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let payload =
            json!({"action":"create","name":"demo","content":"---\ndescription: x\n---\n"});
        let record = stage_skill_write(dir.path(), payload, "create demo", "foreground");
        let pending = list_pending(dir.path(), SUBSYSTEM_SKILLS);
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].id, record.id);
    }

    #[test]
    fn gate_stages_when_write_approval_on() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let payload = json!({"action":"create","name":"demo","content":"body"});
        match maybe_gate_skill_manage(dir.path(), payload, true) {
            SkillManageGate::Staged(msg) => assert!(msg.contains("Pending id:")),
            SkillManageGate::Allow => panic!("expected staged"),
        }
        assert_eq!(list_pending(dir.path(), SUBSYSTEM_SKILLS).len(), 1);
    }

    #[test]
    fn gate_allows_when_write_approval_off() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let payload = json!({"action":"create","name":"demo","content":"body"});
        match maybe_gate_skill_manage(dir.path(), payload, false) {
            SkillManageGate::Allow => {}
            SkillManageGate::Staged(_) => panic!("expected allow"),
        }
        assert!(list_pending(dir.path(), SUBSYSTEM_SKILLS).is_empty());
    }

    #[test]
    fn pending_subcommand_routes() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let ctx = SkillsSubcommandContext {
            write_approval: false,
            inline_shell: false,
            set_write_approval: None,
            set_inline_shell: None,
        };
        assert!(handle_skills_pending_subcommand(dir.path(), "list", &ctx).is_none());
        assert!(handle_skills_pending_subcommand(dir.path(), "pending", &ctx).is_some());
    }
}