debug-parse9.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const fs = require('fs');
  2. const content = fs.readFileSync('temp/rich-outline-fail_57_1779015509994.txt', 'utf-8');
  3. const marker = '========== AI 原始响应 ';
  4. const markerIdx = content.indexOf(marker);
  5. const headerEndIdx = content.indexOf(' ==========', markerIdx);
  6. const aiStart = content.indexOf('\n', headerEndIdx) + 1;
  7. const aiEnd = content.indexOf('\n\n', aiStart);
  8. const aiResponse = content.substring(aiStart, aiEnd);
  9. console.log('AI response length:', aiResponse.length);
  10. // Check position 1624
  11. const pos = 1624;
  12. console.log('\n=== Context around pos', pos, '===');
  13. console.log(JSON.stringify(aiResponse.substring(pos-80, pos+80)));
  14. // Count bad patterns
  15. const badPattern = /,{number":/g;
  16. const matches = aiResponse.match(badPattern);
  17. console.log('\nBad pattern count:', matches ? matches.length : 0);
  18. // What is at position 1624?
  19. console.log('\nChars at', pos, 'to', pos+10, ':');
  20. for (let i = pos; i < pos+10; i++) {
  21. console.log(' pos', i, ':', JSON.stringify(aiResponse[i]), 'code:', aiResponse.charCodeAt(i));
  22. }
  23. // Try with different fix patterns
  24. function fixMissingQuotes(jsonStr) {
  25. return jsonStr
  26. .replace(/,(\{[^"]+":)/g, function(m) { return ',{"' + m.slice(2); })
  27. .replace(/\{number":/g, '{"number":')
  28. .replace(/\{title":/g, '{"title":')
  29. .replace(/\{summary":/g, '{"summary":')
  30. .replace(/\{opening":/g, '{"opening":')
  31. .replace(/\{structure":/g, '{"structure":')
  32. .replace(/\{mustCover":/g, '{"mustCover":')
  33. .replace(/\{mustNotRepeat":/g, '{"mustNotRepeat":')
  34. .replace(/\{toneAdjustment":/g, '{"toneAdjustment":')
  35. .replace(/\{keyTakeaway":/g, '{"keyTakeaway":')
  36. .replace(/\{keyPoints":/g, '{"keyPoints":')
  37. .replace(/\{estimatedWords":/g, '{"estimatedWords":')
  38. .replace(/\{writingInstructions":/g, '{"writingInstructions":');
  39. }
  40. const match = aiResponse.match(/\{[\s\S]*\}/);
  41. if (match) {
  42. const fixed = fixMissingQuotes(match[0]);
  43. try {
  44. const data = JSON.parse(fixed);
  45. console.log('\nFixed+parsed SUCCESS, chapters:', data.chapters?.length);
  46. } catch(e) {
  47. console.log('\nFixed+parsed failed:', e.message.substring(0, 100));
  48. const p = parseInt(e.message.match(/position (\d+)/)?.[1] || '0');
  49. console.log('Error at position', p);
  50. console.log('Context:', JSON.stringify(fixed.substring(Math.max(0,p-50), p+50)));
  51. }
  52. }