Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
346 lines (295 loc) · 12.7 KB
/
Copy pathcli.py
File metadata and controls
346 lines (295 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import argparse
import asyncio
import io
import json
import logging
import re
import sys
import warnings
from collections.abc import Iterator
from importlib.util import find_spec
from pathlib import Path
from shutil import rmtree
from typing import Literal
from model2vec.utils import get_package_extras
from semble.cache import cache_key, resolve_cache_folder, save_index_to_cache
from semble.index import SembleIndex
from semble.index.types import PersistencePath
from semble.installer.agents import AGENTS, IntegrationType
from semble.stats import format_savings_report
from semble.types import ContentType
from semble.utils import format_results, is_git_url, resolve_chunk
from semble.version import __version__
_CLI_DISPATCH_ARGS = frozenset(
{"search", "find-related", "install", "uninstall", "savings", "-h", "--help", "clear", "--version", "-V"}
)
_CLEAR_CHOICE = Literal["all", "index", "savings", "orphans"]
_SHA_256_REGEX = re.compile(r"^[a-f0-9]{64}$")
def _build_index(path: str, content: list[ContentType]) -> SembleIndex:
"""Build an index from a local path or git URL, showing a progress bar on a tty."""
show_progress_bar = sys.stderr.isatty() # Only show the progress bar in a terminal
return (
SembleIndex.from_git(path, content=content, show_progress_bar=show_progress_bar)
if is_git_url(path)
else SembleIndex.from_path(path, content=content, show_progress_bar=show_progress_bar)
)
def _maybe_save_index(parts: list[tuple[str, SembleIndex]]) -> None:
"""Save each freshly built (path, index) part to the cache folder under its path."""
for path, index in parts:
try:
save_index_to_cache(index, path)
except Exception as e:
print(f"Error saving index: {e}", file=sys.stderr)
def _add_query_args(p: argparse.ArgumentParser) -> None:
"""Add the path, result-shaping, and content arguments shared by search and find-related."""
p.add_argument(
"path",
nargs="*",
default=["."],
help="Local paths or git URLs to search together (default: current directory).",
)
p.add_argument("-k", "--top-k", type=int, default=5, help="Number of results (default: 5).")
p.add_argument(
"--max-snippet-lines",
type=int,
default=None,
metavar="N",
help="Lines of source per result (default: full chunk). 10 = signature + body, 0 = no code.",
)
p.add_argument("--format", choices=["json", "text"], default="json", help="Output format (default: json).")
_add_content_args(p)
def _add_content_args(p: argparse.ArgumentParser) -> None:
"""Add --content and deprecated --include-text-files to a subparser."""
p.add_argument(
"--content",
nargs="+",
default=["code"],
choices=[ct.value for ct in ContentType] + ["all"],
metavar="TYPE",
help="Content types to search (space-separated, e.g. --content code docs). Choices: code, docs, config, all. Default: code.",
)
p.add_argument(
"--include-text-files",
action="store_true",
help="Deprecated. Use --content all instead.",
)
def main() -> None:
"""Entry point for the semble command-line tool."""
# Non-UTF-8 Windows consoles can't encode glyphs like "✓" and would otherwise crash.
for stream in (sys.stdout, sys.stderr):
if isinstance(stream, io.TextIOWrapper):
stream.reconfigure(errors="replace")
if len(sys.argv) > 1 and sys.argv[1] in _CLI_DISPATCH_ARGS:
_cli_main()
else:
_mcp_main()
def _mcp_main() -> None:
parser = argparse.ArgumentParser(
prog="semble",
description="Instant local code search for agents.",
)
_add_content_args(parser)
args = parser.parse_args()
if any(find_spec(dep) is None for dep in get_package_extras("semble", "mcp")):
print("MCP dependencies are not installed. Run: pip install 'semble[mcp]'", file=sys.stderr)
raise SystemExit(1)
from semble.mcp import serve
content = _resolve_content(args.content, args.include_text_files)
asyncio.run(serve(content))
def _resolve_content(content: list[str], include_text_files: bool) -> list[ContentType]:
"""Resolve --content and the deprecated --include-text-files into a list of ContentType values."""
if include_text_files:
warnings.warn(
"--include-text-files is deprecated and will be removed in a future version. Use --content all instead.",
DeprecationWarning,
stacklevel=2,
)
if include_text_files or "all" in content:
return [ContentType.CODE, ContentType.DOCS, ContentType.CONFIG]
return [ContentType(c) for c in content]
def _load_index(paths: list[str], content: list[ContentType]) -> tuple[SembleIndex, list[tuple[str, SembleIndex]]]:
"""Build an index per path or git URL, exiting on FileNotFoundError.
:param paths: Local paths or git URLs to index.
:param content: Content types to include.
:return: The index to search (merged when several paths are given) and the per-path (path, index) parts.
"""
try:
parts = [(path, _build_index(path, content)) for path in paths]
except FileNotFoundError as e:
print(str(e), file=sys.stderr)
sys.exit(1)
return (parts[0][1] if len(parts) == 1 else SembleIndex.merge(parts)), parts
def _print_results(out: dict, output_format: str) -> None:
"""Print a format_results() payload as JSON or as human-readable text."""
if output_format == "json":
print(json.dumps(out))
elif "error" in out:
print(out["error"])
else:
for r in out["results"]:
snippet = f"\n\n{r['content']}" if "content" in r else ""
print(f"{r['file_path']}:{r['start_line']}-{r['end_line']}{snippet}\n")
def _run_search(
paths: list[str],
query: str,
top_k: int,
content: list[ContentType],
max_snippet_lines: int | None,
output_format: str,
) -> None:
"""Handle the `search` subcommand."""
index, parts = _load_index(paths, content)
results = index.search(query, top_k=top_k, max_snippet_lines=max_snippet_lines)
out = (
format_results(query, results, max_snippet_lines, index.sources) if results else {"error": "No results found."}
)
_print_results(out, output_format)
_maybe_save_index(parts)
def _run_find_related(
paths: list[str],
file_path: str,
line: int,
top_k: int,
content: list[ContentType],
max_snippet_lines: int | None,
output_format: str,
) -> None:
"""Handle the `find-related` subcommand."""
index, parts = _load_index(paths, content)
chunk = resolve_chunk(index.chunks, file_path, line)
if chunk is None:
print(f"No chunk found at {file_path}:{line}.", file=sys.stderr)
sys.exit(1)
results = index.find_related(chunk, top_k=top_k, max_snippet_lines=max_snippet_lines)
label = f"Chunks related to {file_path}:{line}"
out = (
format_results(label, results, max_snippet_lines, index.sources)
if results
else {"error": f"No related chunks found for {file_path}:{line}."}
)
_print_results(out, output_format)
_maybe_save_index(parts)
def _cached_index_paths(cache_folder: Path) -> Iterator[Path]:
"""Yield index folders in the cache that sit under a sha256 cache key."""
return (path for path in cache_folder.glob("*/index*") if _SHA_256_REGEX.match(path.parent.name))
def _clear_indexes(cache_folder: Path) -> None:
"""Remove all valid index entries from the cache folder."""
indexes = {
path.parent for path in _cached_index_paths(cache_folder) if not PersistencePath.from_path(path).non_existing()
}
if not indexes:
print(f"No indexes found to clear in `{cache_folder}`")
else:
for index_folder in indexes:
rmtree(index_folder)
print(f"Cleared index at `{index_folder}`")
def _clear_savings(cache_folder: Path) -> None:
"""Remove the savings file from the cache folder."""
path = cache_folder / "savings.jsonl"
if not path.exists():
print(f"No savings file found at `{path}`")
else:
path.unlink()
print(f"Cleared savings at `{path}`")
def _clear_orphans(cache_folder: Path) -> None:
"""Remove index entries whose local root_path no longer exists."""
orphans: dict[Path, str] = {}
for path in _cached_index_paths(cache_folder):
try:
with open(path / "metadata.json", encoding="utf-8") as f:
metadata = json.load(f)
root_path = metadata.get("root_path") if isinstance(metadata, dict) else None
except (OSError, json.JSONDecodeError):
continue
# Git-URL entries store their temp clone dir as root_path, so only trust entries whose key matches.
if not isinstance(root_path, str) or not root_path or cache_key(root_path) != path.parent.name:
continue
if not Path(root_path).exists():
orphans[path.parent] = root_path
if not orphans:
print("No orphaned indexes found")
else:
for index_folder, root_path in orphans.items():
rmtree(index_folder)
print(f"Cleared orphaned index for `{root_path}`")
def _run_clear(clear_type: _CLEAR_CHOICE) -> None:
"""Run the `clear` subcommand."""
cache_folder = resolve_cache_folder()
if clear_type == "index" or clear_type == "all":
_clear_indexes(cache_folder)
if clear_type == "savings" or clear_type == "all":
_clear_savings(cache_folder)
if clear_type == "orphans":
_clear_orphans(cache_folder)
def _cli_main() -> None:
logging.basicConfig(level=logging.WARNING, format="%(levelname)s: %(message)s")
parser = argparse.ArgumentParser(prog="semble")
parser.add_argument("-V", "--version", action="version", version=__version__)
sub = parser.add_subparsers(dest="command")
search_p = sub.add_parser("search", help="Search a codebase.")
search_p.add_argument("query", help="Natural language or code query.")
_add_query_args(search_p)
clear_p = sub.add_parser("clear", help="Clear the index cache.")
clear_p.add_argument(
"type",
choices=["all", "index", "savings", "orphans"],
help="Type of cache to clear. `orphans` removes indexes whose source path no longer exists.",
)
related_p = sub.add_parser("find-related", help="Find code similar to a specific location.")
related_p.add_argument("file_path", help="File path as shown in search results.")
related_p.add_argument("line", type=int, help="Line number (1-indexed).")
_add_query_args(related_p)
sub.add_parser("savings", help="Show token savings and usage stats.")
install_p = sub.add_parser("install", help="Configure semble across coding agents.")
uninstall_p = sub.add_parser("uninstall", help="Remove semble configuration from coding agents.")
for p, verb in ((install_p, "configure"), (uninstall_p, "remove configuration from")):
p.add_argument(
"--agent",
nargs="+",
choices=[a.id for a in AGENTS],
metavar="AGENT",
help=f"Agent(s) to {verb} non-interactively, e.g. --agent claude pi. Skips prompts.",
)
p.add_argument(
"--type",
nargs="+",
choices=[*(t.value for t in IntegrationType), "all"],
metavar="TYPE",
help="Integrations to include (mcp, instructions, subagent, or all). Default: all. Requires --agent.",
)
p.add_argument(
"-y",
"--yes",
action="store_true",
help="Skip the confirmation prompt. Combine with --agent for a fully non-interactive run.",
)
args = parser.parse_args()
if args.command == "savings":
print(format_savings_report())
elif args.command in ("install", "uninstall"):
if args.type and not args.agent:
parser.error("--type requires --agent")
from semble.installer import run
integration_ids = None if not args.type or "all" in args.type else [IntegrationType(t) for t in args.type]
run(args.command, agent_ids=args.agent, integration_ids=integration_ids, yes=args.yes)
elif args.command == "clear":
_run_clear(args.type)
elif args.command == "search":
_run_search(
args.path,
args.query,
args.top_k,
_resolve_content(args.content, args.include_text_files),
args.max_snippet_lines,
args.format,
)
elif args.command == "find-related":
_run_find_related(
args.path,
args.file_path,
args.line,
args.top_k,
_resolve_content(args.content, args.include_text_files),
args.max_snippet_lines,
args.format,
)