Coverage for cli/commands/skill.py: 15%

1"""

2/skill 命令处理模块

3"""

4 from rich.console import Console

5 from rich.table import Table

6 from qrclaw.skills.registry import SkillRegistry

7

8

9 def handle(args: str, console: Console, workspace) -> None:

10 """处理 /skill 子命令。"""

11 parts = args.strip().split(maxsplit=1)

12

13 if not parts or not parts[0]:

14 _print_help(console)

15 return

16

17 subcommand = parts[0]

18 sub_args = parts[1].strip() if len(parts) > 1 else ""

19

20 if subcommand == "list":

21 _cmd_list(console, workspace)

22 elif subcommand == "import":

23 _cmd_import(sub_args, console)

24 else:

25 console.print(f"[red]未知子命令: {subcommand}[/red]")

26 _print_help(console)

27

28

29 def _cmd_list(console: Console, workspace) -> None:

30 registry = SkillRegistry()

31 registry.load_from_dir(workspace.skills_dir)

32

33 if not registry.skills:

34 console.print("[yellow]没有安装任何技能[/yellow]")

35 console.print("[dim]在对话中说：导入 skill <skill_name>[/dim]")

36 return

37

38 table = Table(title="已安装的技能")

39 table.add_column("名称", style="cyan")

40 table.add_column("描述", style="green")

41 table.add_column("版本", style="yellow")

42

43 for name, skill in registry.skills.items():

44 desc = skill.description

45 table.add_row(

46 name,

47 desc[:50] + "..." if len(desc) > 50 else desc,

48 skill.version,

49 )

50

51 console.print(table)

52

53

54 def _cmd_import(skill_identifier: str, console: Console) -> None:

55 if not skill_identifier:

56 console.print("[red]用法: /skill import <skill_name_or_url>[/red]")

57 console.print("[dim]示例:[/dim]")

58 console.print("[dim] /skill import agent-self-reflection[/dim]")

59 console.print("[dim] /skill import https://github.com/user/skill[/dim]")

60 console.print()

61 console.print("[dim]或者在对话中说：请导入 skill agent-self-reflection[/dim]")

62 return

63

64 console.print(f"[cyan]请在对话中说：导入 skill {skill_identifier}[/cyan]")

65 console.print("[dim]LLM 会使用 install-skill skill 帮你下载[/dim]")

66

67

68 def _print_help(console: Console) -> None:

69 console.print("[dim]用法: /skill <list|import> [参数][/dim]")

70 console.print("[dim] list 列出所有技能[/dim]")

71 console.print("[dim] import <name> 导入技能[/dim]")