import os
import json
import re

log_path = r"C:\Users\Abhiram P M\.gemini\antigravity-ide\brain\cddac411-36f2-4599-a02d-70756e350cf6\.system_generated\logs\transcript_full.jsonl"
os.makedirs("recovered_html3", exist_ok=True)

with open(log_path, "r", encoding="utf-8") as f:
    for i, line in enumerate(f):
        try:
            step = json.loads(line)
            
            # If it's a tool response
            if step.get("type") == "TOOL_RESPONSE":
                content = step.get("content", "")
                
                if "The following code has been modified to include a line number before every line" in content:
                    m = re.search(r"File Path: `file:///(.*?)`", content)
                    if m:
                        filepath = m.group(1).replace("%20", " ")
                        filename = os.path.basename(filepath)
                        
                        if filename.endswith(".html"):
                            lines = content.split("\n")
                            clean_lines = []
                            in_code = False
                            for l in lines:
                                if l.startswith("The following code has been modified"):
                                    in_code = True
                                    continue
                                
                                if in_code:
                                    match = re.match(r"^\d+:\s?(.*)", l)
                                    if match:
                                        clean_lines.append(match.group(1))
                                    else:
                                        clean_lines.append(l)
                                        
                            full_html = "\n".join(clean_lines)
                            
                            with open(os.path.join("recovered_html3", filename), "w", encoding="utf-8") as out:
                                out.write(full_html)
                            print(f"Recovered {filename} from step {i} ({len(full_html)} bytes)")
        except Exception as e:
            pass
