const fs = require('fs');
const readline = require('readline');

async function processTranscript() {
    const fileStream = fs.createReadStream('C:\\Users\\BS\\.gemini\\antigravity\\brain\\393257b2-e812-421c-92f7-099f2362049e\\.system_generated\\logs\\transcript.jsonl');
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    if (!fs.existsSync('d:/برنامج مخازن/html_backups')) {
        fs.mkdirSync('d:/برنامج مخازن/html_backups');
    }

    let count = 0;

    for await (const line of rl) {
        try {
            const entry = JSON.parse(line);
            const time = entry.created_at;
            
            function extractHugeStrings(obj) {
                if (typeof obj === 'string') {
                    if (obj.length > 50000 && obj.includes('<html') && obj.includes('<body')) {
                        console.log("Found HTML string of length", obj.length, "at", time);
                        fs.writeFileSync('d:/برنامج مخازن/html_backups/index_' + count + '_' + new Date(time).getTime() + '.html', obj);
                        count++;
                    }
                } else if (Array.isArray(obj)) {
                    for (const item of obj) extractHugeStrings(item);
                } else if (typeof obj === 'object' && obj !== null) {
                    for (const key in obj) extractHugeStrings(obj[key]);
                }
            }
            extractHugeStrings(entry);
            
        } catch (e) {
            // Ignore
        }
    }
    
    console.log("Finished parsing HTMLs. Total:", count);
}

processTranscript();
