Coverage for src/lilbee/cli/commands/meta.py: 100%

72 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-09-28 17:20 +0000

1"""Version, status, reset, and init commands.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6 

7import typer 

8from rich.text import Text 

9 

10from lilbee.app.reset import perform_reset 

11from lilbee.app.services import reset_store 

12from lilbee.app.status import gather_status 

13from lilbee.app.version import get_version 

14from lilbee.cli import theme 

15from lilbee.cli.app import ( 

16 apply_overrides, 

17 console, 

18 data_dir_option, 

19 global_option, 

20) 

21from lilbee.cli.helpers import json_output, print_prefixed, render_status 

22from lilbee.core.config import cfg 

23from lilbee.core.system import LOCAL_ROOT_DIRNAME 

24from lilbee.data.ingest.ignore import IGNORE_FILENAME, IGNORE_TEMPLATE 

25from lilbee.runtime.lock import ResetRefusedError 

26 

27_yes_option = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt.") 

28 

29 

30def version() -> None: 

31 """Show the lilbee version.""" 

32 ver = get_version() 

33 if cfg.json_mode: 

34 json_output({"command": "version", "version": ver}) 

35 return 

36 console.print(f"lilbee {ver}") 

37 

38 

39def status( 

40 data_dir: Path | None = data_dir_option, 

41 use_global: bool = global_option, 

42) -> None: 

43 """Show indexed documents, paths, and chunk counts.""" 

44 apply_overrides(data_dir=data_dir, use_global=use_global) 

45 # Status only reads the store; don't warm the inference fleet for a read. 

46 cfg.worker_pool_eager_start = False 

47 if cfg.json_mode: 

48 json_output(gather_status().model_dump(exclude_none=True)) 

49 return 

50 render_status(console) 

51 

52 

53def reset( 

54 data_dir: Path | None = data_dir_option, 

55 use_global: bool = global_option, 

56 yes: bool = _yes_option, 

57) -> None: 

58 """Delete all documents and data (full factory reset).""" 

59 apply_overrides(data_dir=data_dir, use_global=use_global) 

60 if not yes: 

61 if cfg.json_mode: 

62 json_output({"error": "Use --yes to confirm reset in JSON mode"}) 

63 raise SystemExit(1) 

64 console.print( 

65 Text.assemble( 

66 ("This will delete ALL documents and data.\n", theme.ERROR_BOLD), 

67 f" Documents: {cfg.documents_dir}\n", 

68 f" Data: {cfg.data_dir}", 

69 ), 

70 soft_wrap=True, 

71 ) 

72 confirmed = typer.confirm("Are you sure?", default=False) 

73 if not confirmed: 

74 console.print("Aborted.") 

75 raise SystemExit(0) 

76 

77 try: 

78 result = perform_reset() 

79 except ResetRefusedError as exc: 

80 if cfg.json_mode: 

81 json_output({"error": str(exc)}) 

82 else: 

83 print_prefixed(console, "Error: ", exc, style=theme.ERROR) 

84 raise SystemExit(1) from None 

85 # Reopen LanceDB against the empty data dir; keep providers loaded. 

86 reset_store() 

87 

88 if cfg.json_mode: 

89 json_output(result.model_dump()) 

90 return 

91 

92 console.print( 

93 f"Reset complete: {result.deleted_docs} document(s), " 

94 f"{result.deleted_data} data item(s) deleted.", 

95 ) 

96 if result.skipped: 

97 console.print( 

98 f"{len(result.skipped)} item(s) could not be deleted (locked or permission denied).", 

99 style=theme.WARNING, 

100 ) 

101 

102 

103def init() -> None: 

104 """Initialize a local .lilbee/ knowledge base in the current directory.""" 

105 root = Path.cwd() / LOCAL_ROOT_DIRNAME 

106 if root.is_dir(): 

107 if cfg.json_mode: 

108 json_output({"command": "init", "path": str(root), "created": False}) 

109 return 

110 console.print(f"Already initialized: {root}", soft_wrap=True) 

111 return 

112 

113 docs = root / "documents" 

114 data = root / "data" 

115 docs.mkdir(parents=True) 

116 data.mkdir(parents=True) 

117 (root / ".gitignore").write_text("data/\n", encoding="utf-8") 

118 (root / IGNORE_FILENAME).write_text(IGNORE_TEMPLATE, encoding="utf-8") 

119 

120 if cfg.json_mode: 

121 json_output({"command": "init", "path": str(root), "created": True}) 

122 return 

123 console.print(f"Initialized local knowledge base at {root}", soft_wrap=True)