debug-parse20.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. let openBraces = 0, closeBraces = 0, openBrackets = 0, closeBrackets = 0;
  10. let inString = false, escapeNext = false;
  11. for (let i = 0; i < aiResponse.length; i++) {
  12. const ch = aiResponse[i];
  13. if (escapeNext) { escapeNext = false; continue; }
  14. if (ch === '\\') { escapeNext = true; continue; }
  15. if (ch === '"') { inString = !inString; continue; }
  16. if (inString) continue;
  17. if (ch === '{') openBraces++;
  18. else if (ch === '}') closeBraces++;
  19. else if (ch === '[') openBrackets++;
  20. else if (ch === ']') closeBrackets++;
  21. }
  22. console.log('Opening braces:', openBraces, 'Closing braces:', closeBraces, 'Diff:', openBraces - closeBraces);
  23. console.log('Opening brackets:', openBrackets, 'Closing brackets:', closeBrackets, 'Diff:', openBrackets - closeBrackets);
  24. console.log('Response ends with:', JSON.stringify(aiResponse.substring(aiResponse.length - 20)));
  25. // If braces are unbalanced, let's try to find where it becomes complete
  26. // by scanning through and finding where brace/bracket levels return to 0
  27. let braceLevel = 0, bracketLevel = 0;
  28. let lastCompletePos = 0;
  29. inString = false;
  30. escapeNext = false;
  31. for (let i = 0; i < aiResponse.length; i++) {
  32. const ch = aiResponse[i];
  33. if (escapeNext) { escapeNext = false; continue; }
  34. if (ch === '\\') { escapeNext = true; continue; }
  35. if (ch === '"') { inString = !inString; continue; }
  36. if (inString) continue;
  37. if (ch === '{') braceLevel++;
  38. else if (ch === '}') braceLevel--;
  39. else if (ch === '[') bracketLevel++;
  40. else if (ch === ']') bracketLevel--;
  41. if (braceLevel === 0 && bracketLevel === 0 && (ch === '}' || ch === ']')) {
  42. // Try parsing up to here
  43. const sub = aiResponse.substring(0, i + 1);
  44. try {
  45. const data = JSON.parse(sub);
  46. if (data.chapters && data.chapters.length > 0) {
  47. lastCompletePos = i + 1;
  48. console.log('Found complete valid JSON at position', i + 1, 'with', data.chapters.length, 'chapters');
  49. }
  50. } catch(e) {}
  51. }
  52. }
  53. console.log('Last complete valid JSON position:', lastCompletePos);
  54. if (lastCompletePos > 0) {
  55. console.log('Partial JSON length:', lastCompletePos, 'of', aiResponse.length, '(' + Math.round(lastCompletePos/aiResponse.length*100) + '%)');
  56. }
  57. // Try to find if we can truncate and still get valid partial JSON
  58. console.log('\n=== Trying truncation approach ===');
  59. // The JSON is truncated (unbalanced). Let me try to find the last complete chapter
  60. const chaptersData = aiResponse.match(/"chapters"\s*:\s*\[([\s\S]*)/);
  61. if (chaptersData) {
  62. console.log('Chapters array starts at position:', aiResponse.indexOf('"chapters"'));
  63. // Try to find where the chapters array closes
  64. let bracketCount = 0;
  65. let chaptersEnd = -1;
  66. inString = false;
  67. escapeNext = false;
  68. const chaptersStartIdx = aiResponse.indexOf('"chapters"');
  69. for (let i = chaptersStartIdx; i < aiResponse.length; i++) {
  70. const ch = aiResponse[i];
  71. if (escapeNext) { escapeNext = false; continue; }
  72. if (ch === '\\') { escapeNext = true; continue; }
  73. if (ch === '"') { inString = !inString; continue; }
  74. if (inString) continue;
  75. if (ch === '[') bracketCount++;
  76. else if (ch === ']') { bracketCount--; if (bracketCount === 0) { chaptersEnd = i; break; } }
  77. }
  78. console.log('Chapters array ends at position:', chaptersEnd);
  79. if (chaptersEnd > 0) {
  80. const chaptersJson = '{"chapters":' + aiResponse.substring(chaptersStartIdx + 13, chaptersEnd + 1);
  81. console.log('Trying to parse chapters alone...');
  82. try {
  83. const data = JSON.parse(chaptersJson);
  84. console.log('SUCCESS! Chapters count:', data.chapters.length);
  85. } catch(e) {
  86. console.log('FAILED:', e.message.substring(0, 100));
  87. }
  88. }
  89. }