const fs = require('fs');

const filePath = 'temp/rich-outline-fail_57_1779023037497.txt';
const content = fs.readFileSync(filePath, '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);

// Find all problematic characters (control chars except newlines/tabs/cr)
for (let i = 0; i < aiResponse.length; i++) {
  const code = aiResponse.charCodeAt(i);
  if (code < 32 && code !== 10 && code !== 13 && code !== 9) {
    console.log('Control char at', i, ': code', code, 'hex', code.toString(16));
  }
}

// Check characters around 4070-4080
console.log('\nChars around 4070-4090:');
for (let i = 4070; i < 4090; i++) {
  const code = aiResponse.charCodeAt(i);
  const ch = aiResponse[i];
  console.log('pos', i, ': code=', code, 'char=', JSON.stringify(ch));
}

// Check if there's an issue with the content structure
// Position 4074 is },{
const ctx = aiResponse.substring(4040, 4120);
console.log('\nContext 4040-4120:', JSON.stringify(ctx));