Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165"""Command line entry point."""
from __future__ import annotations
import argparseimport jsonimport osimport sys
from . import embedfrom .indexer import buildfrom .paths import DEFAULT_ROOTfrom .store import Store
def _print_hits(hits, show_text: bool, context_lines: int) -> None: if not hits: print("no matches", file=sys.stderr) return for hit in hits: repo = os.path.basename(hit.repo) loc = f"{hit.repo}/{hit.rel}:{hit.start_line}" print(f"{hit.score:.3f} {repo}/{hit.rel}:{hit.start_line}-{hit.end_line}") if show_text: lines = hit.text.split("\n") if context_lines and len(lines) > context_lines: lines = lines[:context_lines] + [f" ... ({len(lines) - context_lines} more lines)"] for line in lines: print(f" {line}") print(f" -> {loc}") print()
def _warn_if_threads_ignored(threads: int | None) -> None: """Say so when --threads cannot take effect.
The thread count is a llama-server launch argument, so it only applies to a server this process starts. `embed.start()` returns at its first readiness check when something is already listening — deliberately, since the server is shared machine-wide and must not be restarted underneath another session — and the flag would otherwise be dropped without a word. """ if threads is not None and embed.is_up(): print( f"note: --threads {threads} has no effect while the embedding server is " "already running; run `code-index stop` first to restart it", file=sys.stderr, )
def cmd_index(args: argparse.Namespace) -> int: roots = args.root or [DEFAULT_ROOT] _warn_if_threads_ignored(args.threads) result = build(roots, threads=args.threads, force=args.force) print( f"indexed {result['files']} files across {result['repos']} repos: " f"{result['total_chunks']} chunks " f"({result['embedded_chunks']} newly embedded, {result['reused']} files reused)" ) if result["skipped"]: parts = ", ".join(f"{v} {k}" for k, v in sorted(result["skipped"].items())) print(f"skipped: {parts}") return 0
def cmd_search(args: argparse.Namespace) -> int: store = Store(dim=embed.EMBED_DIM) if store.vector_count() == 0: print("index is empty; run `code-index index` first", file=sys.stderr) return 1 embed.start(threads=args.threads) query = " ".join(args.query) vec = embed.embed([query], is_query=True)[0] hits = store.search(vec, k=args.k, repo=args.repo, path_filter=args.path)
if args.json: print(json.dumps([ { "score": round(h.score, 4), "repo": h.repo, "path": h.rel, "start_line": h.start_line, "end_line": h.end_line, "text": h.text, } for h in hits ], indent=2)) else: _print_hits(hits, show_text=not args.quiet, context_lines=args.context) return 0
def cmd_status(args: argparse.Namespace) -> int: store = Store(dim=embed.EMBED_DIM) st = store.stats() print(f"server: {'up' if embed.is_up() else 'down'} ({embed.BASE_URL})") print(f"model: {st['model'] or '-'}") print(f"built: {st['built_at'] or '-'}") print(f"progress: {st['progress'] or '-'}") print(f"files: {st['files']}") print(f"chunks: {st['chunks']} (vectors: {st['vectors']})") if st["repos"]: print("repos:") for repo, n in st["repos"]: print(f" {os.path.basename(repo):24} {n:5d} files") return 0
def cmd_serve(args: argparse.Namespace) -> int: _warn_if_threads_ignored(args.threads) embed.start(threads=args.threads) print(f"embedding server up at {embed.BASE_URL}") return 0
def cmd_stop(args: argparse.Namespace) -> int: print("stopped" if embed.stop() else "not running") return 0
def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="code-index", description="Semantic search over local source repositories.", ) parser.add_argument("--threads", type=int, default=None, help="threads for the embedding server (default: all cores; " "applies only when this command starts the server, so " "changing it needs a `stop` first)") sub = parser.add_subparsers(dest="command", required=True)
p = sub.add_parser("index", help="build or refresh the index") p.add_argument("--root", action="append", help=f"directory holding git repos (default: {DEFAULT_ROOT})") p.add_argument("--force", action="store_true", help="re-embed everything, ignoring unchanged files") p.set_defaults(func=cmd_index)
p = sub.add_parser("search", help="query the index") p.add_argument("query", nargs="+") p.add_argument("-k", type=int, default=10, help="number of results") p.add_argument("--repo", help="restrict to one repository by name") p.add_argument("--path", help="restrict to paths containing this substring") p.add_argument("--context", type=int, default=20, help="max lines of each chunk to print (0 for all)") p.add_argument("-q", "--quiet", action="store_true", help="paths and scores only") p.add_argument("--json", action="store_true", help="machine-readable output") p.set_defaults(func=cmd_search)
p = sub.add_parser("status", help="show index and server state") p.set_defaults(func=cmd_status)
p = sub.add_parser("serve", help="start the embedding server") p.set_defaults(func=cmd_serve)
p = sub.add_parser("stop", help="stop the embedding server") p.set_defaults(func=cmd_stop)
args = parser.parse_args(argv) return args.func(args)
if __name__ == "__main__": raise SystemExit(main())