debug-parse14.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Verify the issue with a minimal fix function
  10. // The issue with my previous fix was: using a function in replace causes issues
  11. // Let's do simple string replacement
  12. const input = 'Hello {world": test';
  13. const output = input.replace("{world":/, '{"world":');
  14. console.log('Simple replacement test:');
  15. console.log('Input:', JSON.stringify(input));
  16. console.log('Output:', JSON.stringify(output));
  17. // Now let's just check if the JSON has any obvious issues by
  18. // checking if ALL property names are properly quoted
  19. let inString = false;
  20. let escapeNext = false;
  21. let braceLevel = 0;
  22. let bracketLevel = 0;
  23. let issues = [];
  24. for (let i = 0; i < aiResponse.length; i++) {
  25. const ch = aiResponse[i];
  26. if (escapeNext) { escapeNext = false; continue; }
  27. if (ch === '\\') { escapeNext = true; continue; }
  28. if (ch === '"') { inString = !inString; continue; }
  29. if (!inString) {
  30. if (ch === '{') {
  31. braceLevel++;
  32. // Check the next non-space character
  33. let j = i + 1;
  34. while (j < aiResponse.length && aiResponse[j] === ' ') j++;
  35. if (j < aiResponse.length && aiResponse[j] !== '"' && aiResponse[j] !== '}') {
  36. issues.push({pos: i, char: ch, nextChar: aiResponse[j], context: aiResponse.substring(Math.max(0,i-20), i+30)});
  37. }
  38. } else if (ch === '}') {
  39. braceLevel--;
  40. } else if (ch === '[') {
  41. bracketLevel++;
  42. } else if (ch === ']') {
  43. bracketLevel--;
  44. }
  45. }
  46. }
  47. console.log('\nIssues found:');
  48. issues.slice(0, 10).forEach(issue => {
  49. console.log(' pos', issue.pos, ': char', JSON.stringify(issue.char), 'next', JSON.stringify(issue.nextChar));
  50. console.log(' context:', JSON.stringify(issue.context));
  51. });
  52. // Try a completely different approach - use a streaming JSON parser to find the error
  53. console.log('\n=== Trying to find exact error location ===');
  54. // Let's manually find the last COMPLETE property before position 1624
  55. const before1624 = aiResponse.substring(0, 1624);
  56. console.log('Last 200 chars before position 1624:');
  57. console.log(JSON.stringify(before1624.substring(before1624.length - 200)));
  58. // Let's check if maybe the issue is a TRUNCATION - the JSON ends in the middle of a string
  59. console.log('\n=== Checking for truncation ===');
  60. console.log('Ends with }:', aiResponse.trim().endsWith('}'));
  61. console.log('Ends with ,:', aiResponse.trim().endsWith(','));
  62. const lastComma = before1624.lastIndexOf(',');
  63. const lastBrace = before1624.lastIndexOf('}');
  64. const lastBracket = before1624.lastIndexOf(']');
  65. console.log('Last comma before 1624:', lastComma, '=', JSON.stringify(before1624[lastComma]));
  66. console.log('Last brace before 1624:', lastBrace);
  67. console.log('Last bracket before 1624:', lastBracket);