import json import re from typing import List, Tuple, Dict def parse_to_files(model_output: str) -> List[Tuple[str, str]]: """ Parses the model’s output (ideally JSON) to a list of (filename, content). Fallback heuristics if output is not strictly JSON. """ # First try JSON parsing try: obj = json.loads(model_output) files = [] for fname, content in obj.items(): files.append((fname, content)) return files except json.JSONDecodeError: # fallback: look for patterns like: # Filename: xyz.py \n```python\n ... \n``` pattern = r"Filename:\s*(?P[\w\.\-/]+)\s*```(?:[a-zA-Z0-9]*)\n(?P[\s\S]*?)```" matches = re.finditer(pattern, model_output) files = [] for m in matches: fname = m.group("fname").strip() code = m.group("code") files.append((fname, code)) if files: return files # ultimate fallback: write everything into a single file return [("all_code.txt", model_output)]