Coverage for src / lilbee / cli / tui / command_registry.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-04-29 19:16 +0000

1"""Single source of truth for TUI slash commands. 

2 

3Every slash command is defined once here. All other modules (chat dispatch, 

4suggester, help modal, autocomplete) read from this registry. 

5""" 

6 

7from __future__ import annotations 

8 

9from dataclasses import dataclass 

10 

11 

12@dataclass(frozen=True) 

13class SlashCommand: 

14 """Definition of a single slash command.""" 

15 

16 name: str 

17 handler: str 

18 aliases: tuple[str, ...] = () 

19 args_hint: str = "" 

20 help_text: str = "" 

21 has_arg_completion: bool = False 

22 

23 

24COMMANDS: tuple[SlashCommand, ...] = ( 

25 SlashCommand( 

26 "/model", 

27 "_cmd_model", 

28 aliases=(), 

29 args_hint="[name]", 

30 help_text="Switch model (no arg = catalog)", 

31 has_arg_completion=True, 

32 ), 

33 SlashCommand( 

34 "/add", 

35 "_cmd_add", 

36 aliases=(), 

37 args_hint="path", 

38 help_text="Add file to knowledge base", 

39 has_arg_completion=True, 

40 ), 

41 SlashCommand( 

42 "/crawl", 

43 "_cmd_crawl", 

44 aliases=(), 

45 args_hint="[url]", 

46 help_text="Crawl a URL (no args = dialog)", 

47 ), 

48 SlashCommand( 

49 "/delete", 

50 "_cmd_delete", 

51 aliases=(), 

52 args_hint="name", 

53 help_text="Remove document from index", 

54 has_arg_completion=True, 

55 ), 

56 SlashCommand( 

57 "/set", 

58 "_cmd_set", 

59 aliases=(), 

60 args_hint="key val", 

61 help_text="Change a setting", 

62 has_arg_completion=True, 

63 ), 

64 SlashCommand( 

65 "/theme", 

66 "_cmd_theme", 

67 aliases=(), 

68 args_hint="name", 

69 help_text="Switch theme", 

70 has_arg_completion=True, 

71 ), 

72 SlashCommand("/reset", "_cmd_reset", args_hint="confirm", help_text="Factory reset"), 

73 SlashCommand("/status", "_cmd_status", help_text="Knowledge base status"), 

74 SlashCommand("/settings", "_cmd_settings", help_text="View/change settings"), 

75 SlashCommand( 

76 "/models", 

77 "_cmd_catalog", 

78 aliases=("/m", "/catalog"), 

79 help_text="Browse catalog", 

80 ), 

81 SlashCommand("/setup", "_cmd_setup", help_text="Run setup wizard"), 

82 SlashCommand( 

83 "/wiki", 

84 "_cmd_wiki", 

85 help_text="Open wiki", 

86 ), 

87 SlashCommand( 

88 "/remove", 

89 "_cmd_remove", 

90 aliases=(), 

91 args_hint="<name>", 

92 help_text="Remove an installed model", 

93 has_arg_completion=True, 

94 ), 

95 SlashCommand("/login", "_cmd_login", args_hint="<token>", help_text="Log in to HuggingFace"), 

96 SlashCommand("/help", "_cmd_help", aliases=("/h",), help_text="Show help"), 

97 SlashCommand("/version", "_cmd_version", help_text="Show version"), 

98 SlashCommand("/cancel", "_cmd_cancel", help_text="Cancel active operations"), 

99 SlashCommand("/clear", "_cmd_clear", help_text="Clear chat history"), 

100 SlashCommand("/quit", "_cmd_quit", aliases=("/q", "/exit"), help_text="Exit"), 

101) 

102 

103 

104def build_dispatch_dict() -> dict[str, str]: 

105 """Build a mapping from command name (and aliases) to handler method name.""" 

106 dispatch: dict[str, str] = {} 

107 for cmd in COMMANDS: 

108 dispatch[cmd.name] = cmd.handler 

109 for alias in cmd.aliases: 

110 dispatch[alias] = cmd.handler 

111 return dispatch 

112 

113 

114def completion_names() -> tuple[str, ...]: 

115 """All command names including aliases, for tab completion.""" 

116 names: list[str] = [] 

117 for cmd in COMMANDS: 

118 names.append(cmd.name) 

119 names.extend(cmd.aliases) 

120 return tuple(names)