debug-parse3.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const fs = require('fs');
  2. const content = fs.readFileSync('temp/rich-outline-fail_57_1779014018760.txt', 'utf-8');
  3. // Check the ORIGINAL tail (last 500 chars as stored in file)
  4. const tailMarker = '========== 响应结尾500字符 ';
  5. const tailIdx = content.indexOf(tailMarker);
  6. const tailStart = content.indexOf('\n', tailIdx) + 1;
  7. const tail = content.substring(tailStart).trim();
  8. console.log('=== File tail (last 500 chars of original response) ===');
  9. console.log(JSON.stringify(tail.substring(0, 500)));
  10. // Now let's try to see if we can manually fix the JSON by:
  11. // 1. Finding the incomplete last object and completing it
  12. const aiResponse = content.split('========== AI 原始响应 ')[1].split('==========')[0].trim();
  13. console.log('\n=== Checking if truncation is the issue ===');
  14. console.log('Saved length:', aiResponse.length, 'File says: 9867');
  15. const lastCompleteProp = '"estimatedWords": 800';
  16. const lastCompleteIdx = aiResponse.lastIndexOf(lastCompleteProp);
  17. console.log('Last complete estimatedWords at:', lastCompleteIdx);
  18. // The response seems to be truncated mid-property
  19. // Let's count levels to see where we are
  20. let braceLevel = 0;
  21. let bracketLevel = 0;
  22. let inString = false;
  23. let escapeNext = false;
  24. let lastCompletePos = lastCompleteIdx + lastCompleteProp.length;
  25. for (let i = 0; i < lastCompleteIdx + lastCompleteProp.length; i++) {
  26. const ch = aiResponse[i];
  27. if (escapeNext) { escapeNext = false; continue; }
  28. if (ch === '\\') { escapeNext = true; continue; }
  29. if (ch === '"') { inString = !inString; continue; }
  30. if (!inString) {
  31. if (ch === '{') braceLevel++;
  32. else if (ch === '}') braceLevel--;
  33. else if (ch === '[') bracketLevel++;
  34. else if (ch === ']') bracketLevel--;
  35. }
  36. }
  37. console.log('At end of last complete prop - Brace level:', braceLevel, 'Bracket level:', bracketLevel);
  38. // Now check what the full structure looks like
  39. // If braceLevel = 0, we need to close several }
  40. console.log('\n=== Simulating truncated JSON completion ===');
  41. // The incomplete part ends with:
  42. // "estimated" (incomplete "estimatedWords": <number>)
  43. const incomplete = ', "estimated"; // truncated here'
  44. // Try to count what we'd need to close
  45. console.log('Missing closing brackets/braces to make valid JSON:');
  46. // Count open structures at truncation point
  47. inString = false;
  48. escapeNext = false;
  49. braceLevel = 0;
  50. bracketLevel = 0;
  51. for (let i = 0; i < aiResponse.length; i++) {
  52. const ch = aiResponse[i];
  53. if (escapeNext) { escapeNext = false; continue; }
  54. if (ch === '\\') { escapeNext = true; continue; }
  55. if (ch === '"') { inString = !inString; continue; }
  56. if (!inString) {
  57. if (ch === '{') braceLevel++;
  58. else if (ch === '}') braceLevel--;
  59. else if (ch === '[') bracketLevel++;
  60. else if (ch === ']') bracketLevel--;
  61. }
  62. }
  63. console.log('At truncation - Open braces:', braceLevel, 'Open brackets:', bracketLevel);