A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Heuristic repository map / explain command.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from dataclasses import dataclass, field |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from agent_context.config import Config |
| 9 | from agent_context.packer import _detect_stack |
| 10 | from agent_context.scanner import FileKind, ScanResult, scan_repository |
| 11 | |
| 12 | |
| 13 | @dataclass |
| 14 | class RepoMap: |
| 15 | """High-level, heuristic repository explanation.""" |
| 16 | |
| 17 | root: Path |
| 18 | stack: list[str] = field(default_factory=list) |
| 19 | architecture: list[tuple[str, str]] = field(default_factory=list) |
| 20 | entry_points: list[str] = field(default_factory=list) |
| 21 | config_files: list[str] = field(default_factory=list) |
| 22 | notes: list[str] = field(default_factory=list) |
| 23 | |
| 24 | def to_dict(self) -> dict: |
| 25 | return { |
| 26 | "root": str(self.root), |
| 27 | "detected_technology": list(self.stack), |
| 28 | "likely_architecture": [ |
| 29 | {"path": p, "role": r} for p, r in self.architecture |
| 30 | ], |
| 31 | "potential_entry_points": list(self.entry_points), |
| 32 | "important_configuration": list(self.config_files), |
| 33 | "notes": list(self.notes), |
| 34 | } |
| 35 | |
| 36 | |
| 37 | _DIR_ROLES: dict[str, str] = { |
| 38 | "src": "Primary source tree", |
| 39 | "app": "Application / UI layer", |
| 40 | "apps": "Application packages", |
| 41 | "pages": "Page / route layer", |
| 42 | "api": "HTTP / API layer", |
| 43 | "routes": "Routing layer", |
| 44 | "controllers": "Request handlers", |
| 45 | "services": "Business logic", |
| 46 | "models": "Data models", |
| 47 | "schemas": "Schemas / validation", |
| 48 | "lib": "Shared infrastructure", |
| 49 | "utils": "Shared utilities", |
| 50 | "helpers": "Shared helpers", |
| 51 | "hooks": "React / framework hooks", |
| 52 | "components": "UI components", |
| 53 | "auth": "Authentication", |
| 54 | "middleware": "Middleware", |
| 55 | "config": "Configuration", |
| 56 | "internal": "Internal packages", |
| 57 | "pkg": "Library packages", |
| 58 | "cmd": "Command entrypoints", |
| 59 | "tests": "Automated tests", |
| 60 | "test": "Automated tests", |
| 61 | "__tests__": "Automated tests", |
| 62 | "docs": "Documentation", |
| 63 | "scripts": "Utility scripts", |
| 64 | "migrations": "Database migrations", |
| 65 | "prisma": "Prisma data layer", |
| 66 | "db": "Database layer", |
| 67 | } |
| 68 | |
| 69 | |
| 70 | _ENTRY_CANDIDATES: tuple[str, ...] = ( |
| 71 | "src/index.ts", |
| 72 | "src/index.js", |
| 73 | "src/index.tsx", |
| 74 | "src/main.ts", |
| 75 | "src/main.js", |
| 76 | "src/main.py", |
| 77 | "src/app.ts", |
| 78 | "src/app.js", |
| 79 | "src/app.py", |
| 80 | "index.ts", |
| 81 | "index.js", |
| 82 | "main.py", |
| 83 | "app.py", |
| 84 | "manage.py", |
| 85 | "server.js", |
| 86 | "server.ts", |
| 87 | "cmd/main.go", |
| 88 | "main.go", |
| 89 | "src/main.rs", |
| 90 | "app/page.tsx", |
| 91 | "app/layout.tsx", |
| 92 | "pages/index.tsx", |
| 93 | "pages/index.js", |
| 94 | "src/agent_context/cli.py", |
| 95 | "src/agent_context/__main__.py", |
| 96 | ) |
| 97 | |
| 98 | |
| 99 | def explain_repository(root: Path, *, config: Config | None = None, scan: ScanResult | None = None) -> RepoMap: |
| 100 | """Build a heuristic repository map. Never claims certainty.""" |
| 101 | result = scan or scan_repository(root, config=config) |
| 102 | stack = _detect_stack(result) |
| 103 | |
| 104 | # Directory roles from top-level and src/* folders. |
| 105 | architecture: list[tuple[str, str]] = [] |
| 106 | dir_names: set[str] = set() |
| 107 | for f in result.files: |
| 108 | parts = Path(f.rel_path).parts |
| 109 | if parts: |
| 110 | dir_names.add(parts[0]) |
| 111 | if len(parts) >= 2 and parts[0] in {"src", "app", "apps", "lib", "pkg"}: |
| 112 | dir_names.add(f"{parts[0]}/{parts[1]}") |
| 113 | |
| 114 | for name in sorted(dir_names): |
| 115 | key = name.split("/")[-1].lower() |
| 116 | role = _DIR_ROLES.get(key) |
| 117 | if role: |
| 118 | architecture.append((name, role)) |
| 119 | |
| 120 | # Entry points that actually exist. |
| 121 | existing = {f.rel_path.replace("\\", "/") for f in result.files} |
| 122 | entry_points = [c for c in _ENTRY_CANDIDATES if c in existing] |
| 123 | |
| 124 | # Also include directories that look like entry layers. |
| 125 | for d in ("src/app", "src/api", "app", "api", "pages", "cmd"): |
| 126 | if any(p == d or p.startswith(d + "/") for p in existing): |
| 127 | if d not in entry_points and f"{d}/" not in entry_points: |
| 128 | entry_points.append(f"{d}/") |
| 129 | |
| 130 | config_files = sorted( |
| 131 | f.rel_path |
| 132 | for f in result.files |
| 133 | if f.kind == FileKind.CONFIG |
| 134 | or f.path.name.lower() |
| 135 | in { |
| 136 | "package.json", |
| 137 | "tsconfig.json", |
| 138 | "pyproject.toml", |
| 139 | "cargo.toml", |
| 140 | "go.mod", |
| 141 | "dockerfile", |
| 142 | "docker-compose.yml", |
| 143 | "docker-compose.yaml", |
| 144 | "next.config.js", |
| 145 | "next.config.mjs", |
| 146 | "next.config.ts", |
| 147 | "requirements.txt", |
| 148 | } |
| 149 | )[:20] |
| 150 | |
| 151 | notes = [ |
| 152 | "This map is heuristic. Labels describe likely roles, not verified architecture.", |
| 153 | "Detected technology is inferred from filenames and manifests only.", |
| 154 | ] |
| 155 | if not stack: |
| 156 | notes.append("Stack detection was weak - few recognizable manifests found.") |
| 157 | if not architecture: |
| 158 | notes.append("Could not infer a clear directory architecture.") |
| 159 | |
| 160 | return RepoMap( |
| 161 | root=result.root, |
| 162 | stack=stack, |
| 163 | architecture=architecture, |
| 164 | entry_points=entry_points, |
| 165 | config_files=config_files, |
| 166 | notes=notes, |
| 167 | ) |
| 168 | |
| 169 | |
| 170 | def format_repo_map(repo_map: RepoMap) -> str: |
| 171 | lines = [ |
| 172 | "Repository Map", |
| 173 | "", |
| 174 | "Detected technology:", |
| 175 | ] |
| 176 | if repo_map.stack: |
| 177 | for tech in repo_map.stack: |
| 178 | lines.append(f" {tech}") |
| 179 | else: |
| 180 | lines.append(" (none confidently detected)") |
| 181 | |
| 182 | lines.extend(["", "Likely architecture:", ""]) |
| 183 | if repo_map.architecture: |
| 184 | for path, role in repo_map.architecture: |
| 185 | lines.append(path) |
| 186 | lines.append(f" {role}") |
| 187 | lines.append("") |
| 188 | else: |
| 189 | lines.append(" (insufficient structure signals)") |
| 190 | lines.append("") |
| 191 | |
| 192 | lines.append("Potential entry points:") |
| 193 | if repo_map.entry_points: |
| 194 | for ep in repo_map.entry_points: |
| 195 | lines.append(f" {ep}") |
| 196 | else: |
| 197 | lines.append(" (none confidently detected)") |
| 198 | |
| 199 | lines.extend(["", "Important configuration:"]) |
| 200 | if repo_map.config_files: |
| 201 | for cfg in repo_map.config_files: |
| 202 | lines.append(f" {cfg}") |
| 203 | else: |
| 204 | lines.append(" (none found)") |
| 205 | |
| 206 | lines.extend(["", "Notes:"]) |
| 207 | for note in repo_map.notes: |
| 208 | lines.append(f" - {note}") |
| 209 | |
| 210 | return "\n".join(lines) + "\n" |