import json import re from pathlib import Path from typing import Dict, List, Optional def list_papers(outputs_dir: str) -> List[str]: """List all paper directories in outputs.""" outputs_path = Path(outputs_dir) if not outputs_path.exists(): return [] papers = [] for item in outputs_path.iterdir(): if item.is_dir() and not item.name.startswith("."): papers.append(item.name) return sorted(papers) def list_compression_levels(outputs_dir: str, paper_name: str) -> List[int]: """List available compression levels (token limits) for a paper.""" paper_path = Path(outputs_dir) / paper_name if not paper_path.exists(): return [] levels = set() for file in paper_path.glob("distilled_*.txt"): match = re.search(r"distilled_(\d+)\.txt", file.name) if match: levels.add(int(match.group(1))) return sorted(list(levels), reverse=True) def load_distilled( outputs_dir: str, paper_name: str, token_limit: int ) -> Optional[str]: """Load distilled summary text for a paper at given compression level.""" file_path = Path(outputs_dir) / paper_name / f"distilled_{token_limit}.txt" if not file_path.exists(): return None try: return file_path.read_text(encoding="utf-8") except Exception: return None def load_theorem(outputs_dir: str, paper_name: str, token_limit: int) -> Optional[str]: """Load derived theorem markdown for a paper at given compression level.""" file_path = Path(outputs_dir) / paper_name / f"theorem_{token_limit}.md" if not file_path.exists(): return None try: return file_path.read_text(encoding="utf-8") except Exception: return None def load_result(outputs_dir: str, paper_name: str, token_limit: int) -> Optional[Dict]: """Load analysis result JSON for a paper at given compression level.""" file_path = Path(outputs_dir) / paper_name / f"result_{token_limit}.json" if not file_path.exists(): return None try: with open(file_path, "r", encoding="utf-8") as f: return json.load(f) except Exception: return None def load_prompt(prompts_dir: str, agent_type: str, prompt_file: str) -> Optional[str]: """Load a prompt file for a given agent type.""" file_path = Path(prompts_dir) / agent_type / prompt_file if not file_path.exists(): return None try: return file_path.read_text(encoding="utf-8") except Exception: return None