debug-parse11.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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('Length:', aiResponse.length);
  10. // Test the raw response without any fixes
  11. console.log('\n=== TEST 1: Raw response ===');
  12. try {
  13. const data = JSON.parse(aiResponse);
  14. console.log('SUCCESS, chapters:', data.chapters?.length);
  15. } catch(e) {
  16. console.log('FAILED:', e.message.substring(0, 80));
  17. }
  18. // Test with the regex match
  19. const match = aiResponse.match(/\{[\s\S]*\}/);
  20. console.log('\n=== TEST 2: Regex match ===');
  21. if (match) {
  22. console.log('Match length:', match[0].length);
  23. console.log('Match starts with:', JSON.stringify(match[0].substring(0, 50)));
  24. console.log('Match ends with:', JSON.stringify(match[0].substring(match[0].length - 50)));
  25. try {
  26. const data = JSON.parse(match[0]);
  27. console.log('SUCCESS, chapters:', data.chapters?.length);
  28. } catch(e) {
  29. console.log('FAILED:', e.message.substring(0, 80));
  30. }
  31. } else {
  32. console.log('No match found');
  33. }
  34. // Let's test the actual structure around position 1624
  35. console.log('\n=== TEST 3: Structural analysis ===');
  36. const pos = 1624;
  37. console.log('At pos', pos, ':');
  38. console.log(' Char:', JSON.stringify(aiResponse[pos]), 'code:', aiResponse.charCodeAt(pos));
  39. // Find the last complete property before pos 1624
  40. // Look for patterns like "propertyName":value or "propertyName":[" or "propertyName":{
  41. const beforeError = aiResponse.substring(0, pos);
  42. const lastPropMatch = beforeError.match(/"([^"]+)":\s*[^\[{,"]+$/);
  43. console.log('Last property value before error:', lastPropMatch ? lastPropMatch[0] : 'none');
  44. // Let's also check if there's a 0x00 embedded null byte that didn't show up in our check
  45. console.log('\n=== TEST 4: Byte-level analysis ===');
  46. const bytes = Buffer.from(aiResponse, 'utf8');
  47. console.log('Buffer length:', bytes.length);
  48. for (let i = 1620; i < 1630; i++) {
  49. console.log(' byte', i, ':', bytes[i], '(char:', String.fromCharCode(bytes[i]), ')');
  50. }
  51. // Check the original file for any 0x00 bytes that might be stripped
  52. console.log('\n=== TEST 5: Original file check ===');
  53. const fileBytes = fs.readFileSync('temp/rich-outline-fail_57_1779015509994.txt');
  54. console.log('File size:', fileBytes.length);
  55. const aiStartMarker = '========== AI 原始响应 ';
  56. const aiStartIdx = fileBytes.indexOf(aiStartMarker);
  57. const headerEndIdxBytes = fileBytes.indexOf(Buffer.from(' =========='), aiStartIdx);
  58. const aiContentStart = fileBytes.indexOf(Buffer.from('\n'), headerEndIdxBytes) + 1;
  59. const aiContentEnd = fileBytes.indexOf(Buffer.from('\n\n========== 响应结尾'), aiContentStart);
  60. console.log('AI content start offset:', aiContentStart, 'end offset:', aiContentEnd);
  61. console.log('AI content length from file:', aiContentEnd - aiContentStart);
  62. // Check for null bytes in the file
  63. let nullCount = 0;
  64. for (let i = aiContentStart; i < aiContentEnd; i++) {
  65. if (fileBytes[i] === 0) nullCount++;
  66. }
  67. console.log('Null bytes in AI content:', nullCount);