const fs = require('fs'); const content = fs.readFileSync('temp/rich-outline-fail_57_1779014691106.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); const pos = 523; console.log('\n=== Position', pos, 'context ==='); console.log(JSON.stringify(aiResponse.substring(Math.max(0,pos-80), pos+80))); // Check what char is at position 523 console.log('\nChar at 523:', JSON.stringify(aiResponse[523])); console.log('Char code at 523:', aiResponse.charCodeAt(523)); // Check if position 523 is inside a string let inString = false; let escapeNext = false; let lastQuotePos = -1; for (let i = 0; i < pos; i++) { const ch = aiResponse[i]; if (escapeNext) { escapeNext = false; continue; } if (ch === '\\') { escapeNext = true; continue; } if (ch === '"') { inString = !inString; lastQuotePos = i; } } console.log('At pos 523, inString:', inString, 'lastQuotePos:', lastQuotePos); // First 200 chars console.log('\n=== First 200 chars ==='); console.log(JSON.stringify(aiResponse.substring(0, 200))); // Check for console.log('\nContains :', aiResponse.includes('')); console.log('Starts with {:', aiResponse.trim().startsWith('{')); // Try the fixed think tag regex const thinkTagMatch = aiResponse.match(/^[\s\S]*?<\/think>\s*/i); console.log('thinkTagMatch found:', !!thinkTagMatch); // Try strategy 1: direct parse try { const data = JSON.parse(aiResponse); console.log('Direct parse SUCCESS, chapters:', data.chapters?.length); } catch(e) { console.log('Direct parse failed:', e.message.substring(0, 80)); } // Try strategy 2: remove code blocks let cleaned = aiResponse.replace(/```json\s*/g, '').replace(/```\s*/g, ''); try { const data = JSON.parse(cleaned); console.log('After codeblock removal SUCCESS, chapters:', data.chapters?.length); } catch(e) { console.log('Strategy 2 failed:', e.message.substring(0, 80)); } // Strategy 3: regex extract const match = aiResponse.match(/\{[\s\S]*\}/); if (match) { try { const data = JSON.parse(match[0]); console.log('Strategy 3 SUCCESS, chapters:', data.chapters?.length); } catch(e) { console.log('Strategy 3 failed:', e.message.substring(0, 80)); } } else { console.log('Strategy 3: no JSON object found'); }