debug-parse28.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Find chapters array - need to find the correct start
  10. const chaptersArrayStart = aiResponse.indexOf('"chapters":[');
  11. console.log('Chapters array starts at:', chaptersArrayStart);
  12. // Check the raw characters around position 376
  13. console.log('\nChars around position 376:');
  14. for (let i = 370; i < 390; i++) {
  15. console.log(' pos', i, ':', JSON.stringify(aiResponse[i]), 'code:', aiResponse.charCodeAt(i));
  16. }
  17. // Check chars around position 5000
  18. console.log('\nChars around position 5000:');
  19. for (let i = 4995; i < 5010; i++) {
  20. console.log(' pos', i, ':', JSON.stringify(aiResponse[i]), 'code:', aiResponse.charCodeAt(i));
  21. }
  22. // The issue is the findArrayEnd function must be finding the WRONG closing bracket
  23. // Let's trace through it manually
  24. console.log('\n=== Manual trace of bracket counting ===');
  25. let bracketDepth = 0;
  26. let inString = false;
  27. let escapeNext = false;
  28. let lastCloseBracket = -1;
  29. let lastCloseBracketContext = '';
  30. for (let i = chaptersArrayStart + 12; i < chaptersArrayStart + 500; i++) {
  31. const ch = aiResponse[i];
  32. if (escapeNext) { escapeNext = false; continue; }
  33. if (ch === '\\') { escapeNext = true; continue; }
  34. if (ch === '"') { inString = !inString; continue; }
  35. if (inString) continue;
  36. if (ch === '[') { bracketDepth++; console.log(' +bracket at', i, 'now depth', bracketDepth); }
  37. else if (ch === ']') {
  38. bracketDepth--;
  39. lastCloseBracket = i;
  40. lastCloseBracketContext = aiResponse.substring(Math.max(0, i-30), i+30);
  41. console.log(' -bracket at', i, 'now depth', bracketDepth, 'context:', JSON.stringify(lastCloseBracketContext));
  42. if (bracketDepth === 0) { console.log(' Found array end at', i); break; }
  43. }
  44. }
  45. // So the first ] when depth becomes 0 is at position 376 - but that can't be right for 65 chapters
  46. // The issue is we're starting to count from AFTER "chapters":[
  47. // We should start counting from the [ itself
  48. console.log('\n=== Re-trace starting from the opening [ ===');
  49. bracketDepth = 0;
  50. inString = false;
  51. escapeNext = false;
  52. for (let i = chaptersArrayStart + 11; i < chaptersArrayStart + 500; i++) { // Start at 11 to include the [
  53. const ch = aiResponse[i];
  54. if (escapeNext) { escapeNext = false; continue; }
  55. if (ch === '\\') { escapeNext = true; continue; }
  56. if (ch === '"') { inString = !inString; continue; }
  57. if (inString) continue;
  58. if (ch === '[') { bracketDepth++; }
  59. else if (ch === ']') {
  60. bracketDepth--;
  61. if (bracketDepth === 0) {
  62. console.log('Array ends at position', i);
  63. console.log('Context:', JSON.stringify(aiResponse.substring(Math.max(0, i-50), i+50)));
  64. break;
  65. }
  66. }
  67. }
  68. // The correct approach: count from the [ at "chapters":[
  69. console.log('\n=== Correct trace (starting from opening [) ===');
  70. bracketDepth = 0;
  71. inString = false;
  72. escapeNext = false;
  73. const chaptersOpenBracket = chaptersArrayStart + 11; // Position of [ after "chapters":
  74. for (let i = chaptersOpenBracket; i < aiResponse.length; i++) {
  75. const ch = aiResponse[i];
  76. if (escapeNext) { escapeNext = false; continue; }
  77. if (ch === '\\') { escapeNext = true; continue; }
  78. if (ch === '"') { inString = !inString; continue; }
  79. if (inString) continue;
  80. if (ch === '[') bracketDepth++;
  81. else if (ch === ']') {
  82. bracketDepth--;
  83. if (bracketDepth === 0) {
  84. console.log('Array actually ends at position', i);
  85. console.log('This is', i - chaptersOpenBracket, 'characters from the opening [');
  86. console.log('Context:', JSON.stringify(aiResponse.substring(i-100, i+50)));
  87. break;
  88. }
  89. }
  90. }