Read all code files in the directory, return list of {path, content}.
(code_dir: Path)
| 102 | |
| 103 | |
| 104 | def read_code_context(code_dir: Path) -> list[dict[str, str]]: |
| 105 | """Read all code files in the directory, return list of {path, content}.""" |
| 106 | files: list[dict[str, str]] = [] |
| 107 | for fp in sorted(code_dir.rglob("*")): |
| 108 | if not fp.is_file(): |
| 109 | continue |
| 110 | if ".code_backups" in fp.parts or "__pycache__" in fp.parts: |
| 111 | continue |
| 112 | if fp.suffix not in _CODE_EXTENSIONS: |
| 113 | continue |
| 114 | try: |
| 115 | content = fp.read_text(encoding="utf-8", errors="replace") |
| 116 | if len(content) > _MAX_FILE_SIZE: |
| 117 | content = content[:_MAX_FILE_SIZE] + "\n... [truncated]" |
| 118 | files.append({ |
| 119 | "path": str(fp.relative_to(code_dir)), |
| 120 | "content": content, |
| 121 | }) |
| 122 | except Exception: |
| 123 | continue |
| 124 | return files |
| 125 | |
| 126 | |
| 127 | def _format_code_context(files: list[dict[str, str]]) -> str: |
no test coverage detected