debug-parse27.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const fs = require('fs');
  2. const content = fs.readFileSync('temp/rich-outline-fail_57_1779022210168.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. // Find chapters array
  11. const chaptersArrayStart = aiResponse.indexOf('"chapters":[');
  12. console.log('Chapters array starts at:', chaptersArrayStart);
  13. if (chaptersArrayStart === -1) {
  14. console.log('No chapters array found!');
  15. process.exit(1);
  16. }
  17. // Proper bracket counting that respects strings
  18. function findArrayEnd(jsonStr, startIdx) {
  19. let bracketDepth = 0;
  20. let inString = false;
  21. let escapeNext = false;
  22. for (let i = startIdx; i < jsonStr.length; i++) {
  23. const ch = jsonStr[i];
  24. if (escapeNext) { escapeNext = false; continue; }
  25. if (ch === '\\') { escapeNext = true; continue; }
  26. if (ch === '"') { inString = !inString; continue; }
  27. if (inString) continue;
  28. if (ch === '[') { bracketDepth++; }
  29. else if (ch === ']') {
  30. bracketDepth--;
  31. if (bracketDepth === 0) return i;
  32. }
  33. }
  34. return -1;
  35. }
  36. const arrayEnd = findArrayEnd(aiResponse, chaptersArrayStart + 12);
  37. console.log('Chapters array ends at:', arrayEnd);
  38. if (arrayEnd === -1) {
  39. console.log('Could not find array end with proper string handling!');
  40. process.exit(1);
  41. }
  42. const arrayContent = aiResponse.substring(chaptersArrayStart + 12, arrayEnd);
  43. console.log('Array content length:', arrayContent.length);
  44. console.log('Array content start:', JSON.stringify(arrayContent.substring(0, 150)));
  45. console.log('Array content end:', JSON.stringify(arrayContent.substring(arrayContent.length - 100)));
  46. // Fix missing quotes
  47. function fixMissingQuotes(jsonStr) {
  48. return jsonStr
  49. .replace(/\{number":/g, '{"number":')
  50. .replace(/\{title":/g, '{"title":')
  51. .replace(/\{summary":/g, '{"summary":')
  52. .replace(/\{opening":/g, '{"opening":')
  53. .replace(/\{structure":/g, '{"structure":')
  54. .replace(/\{mustCover":/g, '{"mustCover":')
  55. .replace(/\{mustNotRepeat":/g, '{"mustNotRepeat":')
  56. .replace(/\{toneAdjustment":/g, '{"toneAdjustment":')
  57. .replace(/\{keyTakeaway":/g, '{"keyTakeaway":')
  58. .replace(/\{keyPoints":/g, '{"keyPoints":')
  59. .replace(/\{estimatedWords":/g, '{"estimatedWords":')
  60. .replace(/\{writingInstructions":/g, '{"writingInstructions":')
  61. .replace(/\{number:/g, '{"number":')
  62. .replace(/\{title:/g, '{"title":')
  63. .replace(/\{summary:/g, '{"summary":')
  64. .replace(/\{opening:/g, '{"opening":')
  65. .replace(/\{structure:/g, '{"structure":')
  66. .replace(/\{mustCover:/g, '{"mustCover":')
  67. .replace(/\{mustNotRepeat:/g, '{"mustNotRepeat":')
  68. .replace(/\{toneAdjustment:/g, '{"toneAdjustment":')
  69. .replace(/\{keyTakeaway:/g, '{"keyTakeaway":')
  70. .replace(/\{keyPoints:/g, '{"keyPoints":')
  71. .replace(/\{estimatedWords:/g, '{"estimatedWords":')
  72. .replace(/\{writingInstructions:/g, '{"writingInstructions":');
  73. }
  74. const fixedContent = fixMissingQuotes(arrayContent);
  75. // Try to parse the whole array
  76. try {
  77. const chapters = JSON.parse('[' + fixedContent + ']');
  78. console.log('Parsed SUCCESS, chapters:', chapters.length);
  79. chapters.slice(0, 5).forEach((ch, i) => {
  80. console.log(' Chapter', i+1, ':', ch.title ? ch.title.substring(0, 40) : 'NO TITLE', 'number:', ch.number);
  81. });
  82. } catch(e) {
  83. console.log('Array parse FAILED:', e.message.substring(0, 100));
  84. // Try extracting chapter by chapter
  85. console.log('\n=== Trying chapter-by-chapter extraction ===');
  86. // Use a regex to find all chapter objects
  87. const chapterRegex = /\{"number":\s*(\d+)[^}]*\}/g;
  88. let match;
  89. let validChapters = [];
  90. while ((match = chapterRegex.exec(fixedContent)) !== null) {
  91. try {
  92. const chapter = JSON.parse(match[0]);
  93. if (chapter && chapter.title) {
  94. validChapters.push(chapter);
  95. console.log(' Found chapter', chapter.number, ':', chapter.title.substring(0, 40));
  96. }
  97. } catch(e) {
  98. // Try to extract just the number and title
  99. const numMatch = match[0].match(/"number":\s*(\d+)/);
  100. const titleMatch = match[0].match(/"title":\s*"([^"]+)"/);
  101. if (numMatch && titleMatch) {
  102. validChapters.push({ number: parseInt(numMatch[1]), title: titleMatch[1] });
  103. console.log(' Found partial chapter', numMatch[1], ':', titleMatch[1].substring(0, 40));
  104. }
  105. }
  106. }
  107. console.log('\nTotal valid chapters found:', validChapters.length);
  108. }