debug-parse15.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // Check for issues with opening braces
  11. let inString = false;
  12. let escapeNext = false;
  13. let braceLevel = 0;
  14. let issues = [];
  15. for (let i = 0; i < aiResponse.length; i++) {
  16. const ch = aiResponse[i];
  17. if (escapeNext) { escapeNext = false; continue; }
  18. if (ch === '\\') { escapeNext = true; continue; }
  19. if (ch === '"') { inString = !inString; continue; }
  20. if (!inString) {
  21. if (ch === '{') {
  22. braceLevel++;
  23. // Check the next non-space character
  24. let j = i + 1;
  25. while (j < aiResponse.length && aiResponse[j] === ' ') j++;
  26. if (j < aiResponse.length && aiResponse[j] !== '"' && aiResponse[j] !== '}') {
  27. issues.push({pos: i, char: ch, nextChar: aiResponse[j], context: aiResponse.substring(Math.max(0,i-20), i+30)});
  28. }
  29. } else if (ch === '}') {
  30. braceLevel--;
  31. }
  32. }
  33. }
  34. console.log('\nOpening brace issues found:', issues.length);
  35. if (issues.length > 0) {
  36. issues.slice(0, 5).forEach(issue => {
  37. console.log(' pos', issue.pos, ': char', JSON.stringify(issue.char), 'next', JSON.stringify(issue.nextChar));
  38. console.log(' context:', JSON.stringify(issue.context));
  39. });
  40. }
  41. // Check last 200 chars of position 1624
  42. const before1624 = aiResponse.substring(0, 1624);
  43. console.log('\nLast 200 chars before position 1624:');
  44. console.log(JSON.stringify(before1624.substring(before1624.length - 200)));
  45. // Check if the problem is an UNESCAPED comma or something within a string value
  46. // that ends up looking like a property delimiter
  47. console.log('\n=== Checking for string value issues ===');
  48. // Find all unescaped [ or { that appear inside string values
  49. inString = false;
  50. escapeNext = false;
  51. let stringIssues = [];
  52. let lastStringStart = -1;
  53. let stringContent = '';
  54. for (let i = 0; i < aiResponse.length; i++) {
  55. const ch = aiResponse[i];
  56. if (escapeNext) {
  57. escapeNext = false;
  58. stringContent += ch;
  59. continue;
  60. }
  61. if (ch === '\\') {
  62. escapeNext = true;
  63. stringContent += ch;
  64. continue;
  65. }
  66. if (ch === '"') {
  67. if (inString) {
  68. // End of string
  69. inString = false;
  70. } else {
  71. // Start of string
  72. inString = true;
  73. lastStringStart = i;
  74. stringContent = '';
  75. }
  76. continue;
  77. }
  78. if (inString) {
  79. stringContent += ch;
  80. // Check if there's a ,{ pattern inside the string that might cause issues
  81. if (stringContent.endsWith(',{') || stringContent.endsWith(',{')) {
  82. stringIssues.push({pos: i, content: stringContent.slice(-20)});
  83. }
  84. }
  85. }
  86. console.log('Potential string value issues:', stringIssues.length);
  87. if (stringIssues.length > 0) {
  88. stringIssues.slice(0, 3).forEach(si => {
  89. console.log(' pos', si.pos, 'content ending:', JSON.stringify(si.content));
  90. });
  91. }
  92. // The error at position 1624 is a "{" when inString should be false
  93. // Let me check if maybe there's an issue with how I'm reading the file
  94. // Let's just try to print the actual bytes at position 1624 in the original file
  95. console.log('\n=== Re-reading file for byte-level check ===');
  96. const fileContent = fs.readFileSync('temp/rich-outline-fail_57_1779015509994.txt');
  97. const fileContentStr = fileContent.toString('utf-8');
  98. const aiStartIdx = fileContentStr.indexOf(marker);
  99. const headerEndIdxStr = fileContentStr.indexOf(' ==========', aiStartIdx);
  100. const aiStartOffset = fileContentStr.indexOf('\n', headerEndIdxStr) + 1;
  101. const aiEndOffset = fileContentStr.indexOf('\n\n', aiStartOffset);
  102. const aiFromFile = fileContentStr.substring(aiStartOffset, aiEndOffset);
  103. console.log('aiFromFile length:', aiFromFile.length);
  104. console.log('Position 1624 from file:', JSON.stringify(aiFromFile[1624]), 'code:', aiFromFile.charCodeAt(1624));
  105. // Try parsing aiFromFile
  106. try {
  107. JSON.parse(aiFromFile);
  108. console.log('aiFromFile: SUCCESS');
  109. } catch(e) {
  110. console.log('aiFromFile: FAILED -', e.message.substring(0, 80));
  111. const pos = parseInt(e.message.match(/position (\d+)/)?.[1] || '0');
  112. if (pos === 1624) {
  113. console.log('Same error at position 1624 - issue is in the data itself');
  114. }
  115. }