File size: 1,082 Bytes
313dc05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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<fname>[\w\.\-/]+)\s*```(?:[a-zA-Z0-9]*)\n(?P<code>[\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)]