debug-parse10.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // The error is at position 1624 which is the "{" in "{\"number\":3"
  11. // JSON says: Expected double-quoted property name at position 1624
  12. // This means when we hit this "{", we should be seeing a property name, but we're not
  13. // This suggests we might be INSIDE a string when we encounter this "{"
  14. // Let's trace back from position 1624 to find where the string state changes
  15. let inString = false;
  16. let escapeNext = false;
  17. let lastNonSpaceBefore = -1;
  18. for (let i = 0; i < 1624; i++) {
  19. const ch = aiResponse[i];
  20. if (escapeNext) {
  21. escapeNext = false;
  22. lastNonSpaceBefore = i;
  23. continue;
  24. }
  25. if (ch === '\\') {
  26. escapeNext = true;
  27. lastNonSpaceBefore = i;
  28. continue;
  29. }
  30. if (ch === '"') {
  31. inString = !inString;
  32. lastNonSpaceBefore = i;
  33. continue;
  34. }
  35. if (!inString && ch !== ' ' && ch !== '\n' && ch !== '\r' && ch !== '\t') {
  36. lastNonSpaceBefore = i;
  37. }
  38. }
  39. console.log('At position 1624: inString =', inString);
  40. console.log('Last non-space char before 1624: pos', lastNonSpaceBefore, '=', JSON.stringify(aiResponse[lastNonSpaceBefore]));
  41. // Find the nearest opening quote before position 1624 that might be the start of the problematic string
  42. // Look at the last 200 chars before position 1624
  43. console.log('\nLast 200 chars before position 1624:');
  44. console.log(JSON.stringify(aiResponse.substring(1424, 1624)));
  45. // Check for unescaped newlines or special chars in strings
  46. let hasProblem = false;
  47. for (let i = 0; i < 1624; i++) {
  48. const code = aiResponse.charCodeAt(i);
  49. if (code < 32 && code !== 9 && code !== 10 && code !== 13) {
  50. console.log('Problematic char at', i, ': code', code, 'char', JSON.stringify(aiResponse[i]));
  51. hasProblem = true;
  52. }
  53. }
  54. if (!hasProblem) {
  55. console.log('No control characters found before position 1624');
  56. }
  57. // Maybe it's the "numbered" property names issue again but at a nested level
  58. // Let's check all occurrences of problematic patterns
  59. const problemPatterns = [
  60. [',{number":', 'comma opening brace number colon quote'],
  61. [',{title":', 'comma opening brace title colon quote'],
  62. [',{summary":', 'comma opening brace summary colon quote'],
  63. [',{"number":', 'comma opening brace QUOTE number colon quote (correct)'],
  64. ];
  65. for (const [pattern, desc] of problemPatterns) {
  66. let count = 0;
  67. let idx = aiResponse.indexOf(pattern);
  68. while (idx !== -1 && idx < 1624) { count++; idx = aiResponse.indexOf(pattern, idx + 1); }
  69. if (count > 0) console.log(pattern, '->', count, 'times (before pos 1624)');
  70. }
  71. // Let's try a different approach - find all "},{" occurrences and see if any are malformed
  72. const badCommaBrace = /},\{"[^"]+":/g;
  73. let badMatches = [];
  74. let m;
  75. while ((m = badCommaBrace.exec(aiResponse)) !== null) {
  76. if (m.index < 1700) {
  77. badMatches.push({pos: m.index, match: m[0]});
  78. }
  79. }
  80. console.log('\nAll },{" patterns near the error:');
  81. badMatches.forEach(b => console.log(' pos', b.pos, ':', JSON.stringify(b.match.substring(0, 50))));
  82. // Check the actual structure by counting nesting levels
  83. let braceLevel = 0;
  84. let bracketLevel = 0;
  85. inString = false;
  86. escapeNext = false;
  87. for (let i = 0; i < aiResponse.length; i++) {
  88. const ch = aiResponse[i];
  89. if (escapeNext) { escapeNext = false; continue; }
  90. if (ch === '\\') { escapeNext = true; continue; }
  91. if (ch === '"') { inString = !inString; continue; }
  92. if (!inString) {
  93. if (ch === '{') braceLevel++;
  94. else if (ch === '}') braceLevel--;
  95. else if (ch === '[') bracketLevel++;
  96. else if (ch === ']') bracketLevel--;
  97. }
  98. if (i === 1624) {
  99. console.log('\nAt error position 1624: braceLevel =', braceLevel, 'bracketLevel =', bracketLevel, 'inString =', inString);
  100. }
  101. }