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

24 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-06-28 01:01 +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 chat model (no arg opens the 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 or folder to the 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 arg opens the dialog)", 

47 ), 

48 SlashCommand( 

49 "/delete", 

50 "_cmd_delete", 

51 aliases=(), 

52 args_hint="<name>", 

53 help_text="Remove a document from the index", 

54 has_arg_completion=True, 

55 ), 

56 SlashCommand( 

57 "/set", 

58 "_cmd_set", 

59 aliases=(), 

60 args_hint="<key> <value>", 

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 (no arg lists themes)", 

70 has_arg_completion=True, 

71 ), 

72 SlashCommand( 

73 "/reset", 

74 "_cmd_reset", 

75 help_text="Factory reset (asks for confirmation)", 

76 ), 

77 SlashCommand( 

78 "/rebuild", 

79 "_cmd_rebuild", 

80 help_text="Re-index the documents directory from scratch", 

81 ), 

82 SlashCommand( 

83 "/export", 

84 "_cmd_export", 

85 aliases=(), 

86 args_hint="<path>", 

87 help_text="Export a per-page text dataset (parquet or jsonl)", 

88 ), 

89 SlashCommand( 

90 "/import", 

91 "_cmd_import", 

92 aliases=(), 

93 args_hint="<path>", 

94 help_text="Import a per-page text dataset, re-embedding it", 

95 ), 

96 SlashCommand("/status", "_cmd_status", help_text="Show knowledge-base status"), 

97 SlashCommand("/settings", "_cmd_settings", help_text="Open settings"), 

98 SlashCommand( 

99 "/models", 

100 "_cmd_catalog", 

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

102 help_text="Browse the model catalog", 

103 ), 

104 SlashCommand("/setup", "_cmd_setup", help_text="Run the first-run setup wizard"), 

105 SlashCommand( 

106 "/remember", 

107 "_cmd_remember", 

108 args_hint="<text>", 

109 help_text="Save a memory (prefix with 'pref:' for a preference)", 

110 ), 

111 SlashCommand( 

112 "/memories", 

113 "_cmd_memories", 

114 help_text="Browse and manage your saved memories", 

115 ), 

116 SlashCommand( 

117 "/wiki", 

118 "_cmd_wiki", 

119 help_text="Open the wiki view", 

120 ), 

121 SlashCommand( 

122 "/remove", 

123 "_cmd_remove", 

124 aliases=(), 

125 args_hint="<name>", 

126 help_text="Uninstall a downloaded model", 

127 has_arg_completion=True, 

128 ), 

129 SlashCommand( 

130 "/login", 

131 "_cmd_login", 

132 args_hint="[token]", 

133 help_text="Log in to Hugging Face (no arg opens the token page)", 

134 ), 

135 SlashCommand("/help", "_cmd_help", aliases=("/h",), help_text="Show the slash-command catalog"), 

136 SlashCommand("/version", "_cmd_version", help_text="Show the lilbee version"), 

137 SlashCommand("/cancel", "_cmd_cancel", help_text="Cancel any in-flight operations"), 

138 SlashCommand("/clear", "_cmd_clear", help_text="Clear the conversation"), 

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

140) 

141 

142 

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

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

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

146 for cmd in COMMANDS: 

147 dispatch[cmd.name] = cmd.handler 

148 for alias in cmd.aliases: 

149 dispatch[alias] = cmd.handler 

150 return dispatch 

151 

152 

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

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

155 names: list[str] = [] 

156 for cmd in COMMANDS: 

157 names.append(cmd.name) 

158 names.extend(cmd.aliases) 

159 return tuple(names)