const fs = require('fs'); const content = fs.readFileSync('temp/rich-outline-fail_57_1779015509994.txt', 'utf-8'); const marker = '========== AI εŽŸε§‹ε“εΊ” '; const markerIdx = content.indexOf(marker); const headerEndIdx = content.indexOf(' ==========', markerIdx); const aiStart = content.indexOf('\n', headerEndIdx) + 1; const aiEnd = content.indexOf('\n\n', aiStart); const aiResponse = content.substring(aiStart, aiEnd); console.log('AI response length:', aiResponse.length); // My fix function function fixMissingQuotes(jsonStr) { return jsonStr .replace(/\{number":/g, '{"number":') .replace(/\{title":/g, '{"title":') .replace(/\{summary":/g, '{"summary":') .replace(/\{opening":/g, '{"opening":') .replace(/\{structure":/g, '{"structure":') .replace(/\{mustCover":/g, '{"mustCover":') .replace(/\{mustNotRepeat":/g, '{"mustNotRepeat":') .replace(/\{toneAdjustment":/g, '{"toneAdjustment":') .replace(/\{keyTakeaway":/g, '{"keyTakeaway":') .replace(/\{keyPoints":/g, '{"keyPoints":') .replace(/\{estimatedWords":/g, '{"estimatedWords":') .replace(/\{writingInstructions":/g, '{"writingInstructions":'); } // Direct replace test const directFixed = fixMissingQuotes(aiResponse); console.log('\nDirect fix - chars changed:', aiResponse.length - directFixed.split('').filter((c,i) => c !== directFixed[i]).length); // Test direct fix try { const data = JSON.parse(directFixed); console.log('Direct fix: SUCCESS, chapters:', data.chapters?.length); } catch(e) { console.log('Direct fix: FAILED -', e.message.substring(0, 80)); // Check if there's actually a replacement happening const badPattern = /\{number":/g; const count = (aiResponse.match(badPattern) || []).length; console.log('Bad pattern "{number\\": found:', count, 'times in original'); // Try a broader fix approach - replace ,{word": with ,{"word": const broaderFixed = aiResponse.replace(/,(\{[^"]+":)/g, function(m, p1) { return ',{"' + p1.slice(1); }); console.log('Broader fix - chars changed:', aiResponse.length - broaderFixed.split('').filter((c,i) => c !== broaderFixed[i]).length); try { const data = JSON.parse(broaderFixed); console.log('Broader fix: SUCCESS, chapters:', data.chapters?.length); } catch(e2) { console.log('Broader fix: FAILED -', e2.message.substring(0, 80)); // Check the exact position const pos = parseInt(e2.message.match(/position (\d+)/)?.[1] || '0'); console.log('Error at position:', pos); console.log('Context:', JSON.stringify(aiResponse.substring(Math.max(0,pos-50), pos+50))); } } // Also test: what if the problem is not missing quotes but something else? // Let's check if there are any single quotes being used instead of double quotes const singleQuoteProps = /'[^']*':/g; const singleQuoteMatches = aiResponse.match(singleQuoteProps); console.log('\nSingle-quoted properties found:', singleQuoteMatches ? singleQuoteMatches.length : 0); if (singleQuoteMatches && singleQuoteMatches.length < 10) { singleQuoteMatches.forEach(m => console.log(' ', m)); }