debug-parse23.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // The issue: we have more opens than closes, meaning JSON is truncated
  10. // But the actual character at position 1624 looks correct: {"number":3
  11. // Let me check if maybe there's a truncation recovery strategy that could work
  12. // We need to find where the valid JSON ends and try to make sense of it
  13. // Let me check if the response actually contains a valid prefix we can use
  14. // by scanning for valid property structures
  15. // First, let's see if there's any position where we have a COMPLETELY valid prefix
  16. let validUpTo = 0;
  17. for (let i = 1; i <= aiResponse.length; i++) {
  18. try {
  19. const sub = aiResponse.substring(0, i);
  20. JSON.parse(sub);
  21. validUpTo = i;
  22. } catch(e) {
  23. // continue
  24. }
  25. }
  26. console.log('Longest valid prefix length:', validUpTo);
  27. console.log('Valid prefix ends with:', JSON.stringify(aiResponse.substring(validUpTo - 30, validUpTo)));
  28. console.log('Valid prefix length vs total:', validUpTo, '/', aiResponse.length, '=', Math.round(validUpTo/aiResponse.length*100) + '%');
  29. // If we have a long enough valid prefix, we might be able to extract chapters from it
  30. if (validUpTo > 1000) {
  31. console.log('\n=== Valid prefix analysis ===');
  32. const prefix = aiResponse.substring(0, validUpTo);
  33. try {
  34. const data = JSON.parse(prefix);
  35. if (data.mainTheme) console.log('mainTheme found:', data.mainTheme.substring(0, 50));
  36. if (data.chapters) console.log('chapters count in prefix:', data.chapters.length);
  37. } catch(e) {
  38. console.log('Parse failed:', e.message);
  39. }
  40. }
  41. // Now let me try a different approach: count closing braces needed to close the JSON
  42. // and try appending them to see if we can make it parse
  43. console.log('\n=== Trying to complete the truncated JSON ===');
  44. // We have 39 more opens than closes, so we need 39 closing braces
  45. // Let's try to complete the JSON by finding where each unclosed brace should close
  46. // But actually, maybe the better approach is to find where chapters end
  47. // and just try to extract what we can
  48. // Let me try yet another approach: find all complete "chapters": [...] patterns
  49. const chaptersRegex = /"chapters"\s*:\s*\[([\s\S]*?)\]\s*\}/g;
  50. let match;
  51. let foundChapters = [];
  52. while ((match = chaptersRegex.exec(aiResponse)) !== null) {
  53. const inner = match[1];
  54. console.log('Found chapters array at position', match.index, 'length', inner.length);
  55. // Try to parse the chapters array
  56. try {
  57. const arr = JSON.parse('[' + inner + ']');
  58. if (Array.isArray(arr) && arr.length > 0) {
  59. console.log(' Parsed successfully!', arr.length, 'chapters');
  60. foundChapters.push(...arr);
  61. }
  62. } catch(e) {
  63. // Try to extract chapter numbers
  64. const numMatches = inner.match(/"number":(\d+)/g);
  65. console.log(' Chapter numbers found:', numMatches ? numMatches.length : 0);
  66. }
  67. }
  68. console.log('\nTotal chapters found in arrays:', foundChapters.length);