"""读取项目关键文件用于 AI 审查""" import os from pathlib import Path SKIP_DIRS = { "node_modules", ".git", "venv", ".venv", "__pycache__", "dist", "build", "target", ".idea", ".vscode", "vendor", "env", } SKIP_SUFFIXES = { ".png", ".jpg", ".jpeg", ".gif", ".ico", ".pdf", ".zip", ".tar", ".gz", ".whl", ".jar", ".class", ".pyc", ".so", ".dll", ".exe", ".bin", ".woff", ".woff2", ".ttf", ".eot", ".mp4", ".mp3", } MAX_FILE_CHARS = 8000 def iter_source_files(root: str): for dirpath, dirnames, filenames in os.walk(root): dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS] for name in sorted(filenames): path = Path(dirpath) / name if path.suffix.lower() in SKIP_SUFFIXES: continue try: if path.stat().st_size > 512 * 1024: continue if not path.is_file(): continue except OSError: continue yield path def read_key_files(root: str, max_chars: int = 12000) -> str: parts = [] total = 0 for path in iter_source_files(root): try: text = path.read_text(encoding="utf-8", errors="replace") except (OSError, UnicodeDecodeError): continue if len(text) > MAX_FILE_CHARS: text = text[:MAX_FILE_CHARS] + "\n... (截断)" rel = os.path.relpath(path, root) block = f"### 文件: {rel}\n```\n{text}\n```" if total + len(block) > max_chars: break parts.append(block) total += len(block) if len(parts) >= 25: break if not parts: return "(未找到可读源码文件)" return "\n\n".join(parts)