debug-jsonrepair.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. const fs = require('fs');
  2. const { repairJson } = require('jsonrepair');
  3. const content = fs.readFileSync('temp/rich-outline-fail_57_1779023037497.txt', 'utf-8');
  4. const marker = '========== AI 原始响应 ';
  5. const markerIdx = content.indexOf(marker);
  6. const headerEndIdx = content.indexOf(' ==========', markerIdx);
  7. const aiStart = content.indexOf('\n', headerEndIdx) + 1;
  8. const aiEnd = content.indexOf('\n\n', aiStart);
  9. const aiResponse = content.substring(aiStart, aiEnd);
  10. console.log('AI response length:', aiResponse.length);
  11. console.log('Response end:', JSON.stringify(aiResponse.slice(-100)));
  12. // Test strategy 1: jsonrepair directly
  13. try {
  14. const repaired = repairJson(aiResponse);
  15. console.log('\nStrategy 1: repairJson length:', repaired.length);
  16. const data = JSON.parse(repaired);
  17. if (data.chapters && Array.isArray(data.chapters)) {
  18. console.log('Strategy 1 SUCCESS: chapters count =', data.chapters.length);
  19. console.log('First chapter:', JSON.stringify(data.chapters[0]).substring(0, 100));
  20. } else {
  21. console.log('Strategy 1: no chapters in parsed data');
  22. }
  23. } catch(e) {
  24. console.log('\nStrategy 1 FAILED:', e.message.substring(0, 100));
  25. }
  26. // Check if response ends with complete structure
  27. console.log('\nLast 200 chars:', JSON.stringify(aiResponse.slice(-200)));
  28. console.log('Does it end with ]} ?', aiResponse.endsWith(']}'));
  29. console.log('Does it end with ]}] ?', aiResponse.endsWith(']}]'));
  30. console.log('Does it end with ]})] ?', aiResponse.endsWith(']})]'));