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); // Verify the issue with a minimal fix function // The issue with my previous fix was: using a function in replace causes issues // Let's do simple string replacement const input = 'Hello {world": test'; const output = input.replace("{world":/, '{"world":'); console.log('Simple replacement test:'); console.log('Input:', JSON.stringify(input)); console.log('Output:', JSON.stringify(output)); // Now let's just check if the JSON has any obvious issues by // checking if ALL property names are properly quoted let inString = false; let escapeNext = false; let braceLevel = 0; let bracketLevel = 0; let issues = []; for (let i = 0; i < aiResponse.length; i++) { const ch = aiResponse[i]; if (escapeNext) { escapeNext = false; continue; } if (ch === '\\') { escapeNext = true; continue; } if (ch === '"') { inString = !inString; continue; } if (!inString) { if (ch === '{') { braceLevel++; // Check the next non-space character let j = i + 1; while (j < aiResponse.length && aiResponse[j] === ' ') j++; if (j < aiResponse.length && aiResponse[j] !== '"' && aiResponse[j] !== '}') { issues.push({pos: i, char: ch, nextChar: aiResponse[j], context: aiResponse.substring(Math.max(0,i-20), i+30)}); } } else if (ch === '}') { braceLevel--; } else if (ch === '[') { bracketLevel++; } else if (ch === ']') { bracketLevel--; } } } console.log('\nIssues found:'); issues.slice(0, 10).forEach(issue => { console.log(' pos', issue.pos, ': char', JSON.stringify(issue.char), 'next', JSON.stringify(issue.nextChar)); console.log(' context:', JSON.stringify(issue.context)); }); // Try a completely different approach - use a streaming JSON parser to find the error console.log('\n=== Trying to find exact error location ==='); // Let's manually find the last COMPLETE property before position 1624 const before1624 = aiResponse.substring(0, 1624); console.log('Last 200 chars before position 1624:'); console.log(JSON.stringify(before1624.substring(before1624.length - 200))); // Let's check if maybe the issue is a TRUNCATION - the JSON ends in the middle of a string console.log('\n=== Checking for truncation ==='); console.log('Ends with }:', aiResponse.trim().endsWith('}')); console.log('Ends with ,:', aiResponse.trim().endsWith(',')); const lastComma = before1624.lastIndexOf(','); const lastBrace = before1624.lastIndexOf('}'); const lastBracket = before1624.lastIndexOf(']'); console.log('Last comma before 1624:', lastComma, '=', JSON.stringify(before1624[lastComma])); console.log('Last brace before 1624:', lastBrace); console.log('Last bracket before 1624:', lastBracket);