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

async function processLineByLine() {
  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
  });

  let lastAppFinalContent = null;
  let lastAppFinalLength = 0;

  for await (const line of rl) {
    try {
      const obj = JSON.parse(line);
      if (obj.tool_calls) {
        for (const call of obj.tool_calls) {
          if (call.name === 'write_to_file' && call.args && call.args.TargetFile && call.args.TargetFile.endsWith('app_final.js')) {
            if (call.args.CodeContent && call.args.CodeContent.length > 50000) {
                lastAppFinalContent = call.args.CodeContent;
                lastAppFinalLength = call.args.CodeContent.length;
            }
          }
        }
      }
      if (obj.type === 'TOOL_CALL_RESULT' || obj.type === 'TOOL_RESULT') {
         // Also check for view_file or run_command outputs that contain the file
      }
    } catch(e) {}
  }

  if (lastAppFinalContent) {
      fs.writeFileSync('app_final_backup.js', lastAppFinalContent, 'utf8');
      console.log('Found a backup of app_final.js with length: ' + lastAppFinalLength);
  } else {
      console.log('Could not find a full backup in write_to_file calls.');
  }
}

processLineByLine();
