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

62 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-14 11:46 +0000

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

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6 

7import typer 

8 

9from lilbee.app.reset import perform_reset 

10from lilbee.app.services import reset_store 

11from lilbee.app.status import gather_status 

12from lilbee.app.version import get_version 

13from lilbee.cli import theme 

14from lilbee.cli.app import ( 

15 apply_overrides, 

16 console, 

17 data_dir_option, 

18 global_option, 

19) 

20from lilbee.cli.helpers import json_output, render_status 

21from lilbee.core.config import cfg 

22from lilbee.core.system import LOCAL_ROOT_DIRNAME 

23 

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

25 

26 

27def version() -> None: 

28 """Show the lilbee version.""" 

29 ver = get_version() 

30 if cfg.json_mode: 

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

32 return 

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

34 

35 

36def status( 

37 data_dir: Path | None = data_dir_option, 

38 use_global: bool = global_option, 

39) -> None: 

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

41 apply_overrides(data_dir=data_dir, use_global=use_global) 

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

43 cfg.worker_pool_eager_start = False 

44 if cfg.json_mode: 

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

46 return 

47 render_status(console) 

48 

49 

50def reset( 

51 data_dir: Path | None = data_dir_option, 

52 use_global: bool = global_option, 

53 yes: bool = _yes_option, 

54) -> None: 

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

56 apply_overrides(data_dir=data_dir, use_global=use_global) 

57 if not yes: 

58 if cfg.json_mode: 

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

60 raise SystemExit(1) 

61 console.print( 

62 f"[{theme.ERROR_BOLD}]This will delete ALL documents and data.[/{theme.ERROR_BOLD}]\n" 

63 f" Documents: {cfg.documents_dir}\n" 

64 f" Data: {cfg.data_dir}" 

65 ) 

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

67 if not confirmed: 

68 console.print("Aborted.") 

69 raise SystemExit(0) 

70 

71 result = perform_reset() 

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

73 reset_store() 

74 

75 if cfg.json_mode: 

76 json_output(result.model_dump()) 

77 return 

78 

79 console.print( 

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

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

82 ) 

83 if result.skipped: 

84 console.print( 

85 f"[{theme.WARNING}]{len(result.skipped)} item(s) could not be deleted " 

86 f"(locked or permission denied).[/{theme.WARNING}]" 

87 ) 

88 

89 

90def init() -> None: 

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

92 root = Path.cwd() / LOCAL_ROOT_DIRNAME 

93 if root.is_dir(): 

94 if cfg.json_mode: 

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

96 return 

97 console.print(f"Already initialized: {root}") 

98 return 

99 

100 docs = root / "documents" 

101 data = root / "data" 

102 docs.mkdir(parents=True) 

103 data.mkdir(parents=True) 

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

105 

106 if cfg.json_mode: 

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

108 return 

109 console.print(f"Initialized local knowledge base at {root}")