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

const logPath = 'C:/Users/BS/.gemini/antigravity/brain/393257b2-e812-421c-92f7-099f2362049e/.system_generated/logs/transcript.jsonl';

async function extract() {
    const fileStream = fs.createReadStream(logPath);
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    let bestIndex = '';
    let bestAppFinal = '';
    let bestIndexLen = 0;
    let bestAppFinalLen = 0;

    for await (const line of rl) {
        try {
            const entry = JSON.parse(line);
            
            // Look for tool arguments containing the file content
            if (entry.tool_calls) {
                for (const tool of entry.tool_calls) {
                    if (tool.function && tool.function.arguments) {
                        try {
                            const args = JSON.parse(tool.function.arguments);
                            if (args.TargetFile && args.TargetFile.endsWith('index.html') && args.CodeContent) {
                                if (args.CodeContent.length > bestIndexLen) {
                                    bestIndexLen = args.CodeContent.length;
                                    bestIndex = args.CodeContent;
                                }
                            }
                            if (args.TargetFile && args.TargetFile.endsWith('app_final.js') && args.CodeContent) {
                                if (args.CodeContent.length > bestAppFinalLen) {
                                    bestAppFinalLen = args.CodeContent.length;
                                    bestAppFinal = args.CodeContent;
                                }
                            }
                        } catch(e) {}
                    }
                }
            }
        } catch (e) {}
    }

    console.log("Max index.html found in transcript:", bestIndexLen);
    console.log("Max app_final.js found in transcript:", bestAppFinalLen);

    if (bestIndexLen > 0) {
        fs.writeFileSync('d:/برنامج مخازن/index_transcript.html', bestIndex, 'utf8');
    }
    if (bestAppFinalLen > 0) {
        fs.writeFileSync('d:/برنامج مخازن/app_final_transcript.js', bestAppFinal, 'utf8');
    }
}
extract();
