debug-parse19.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Let's try isolating the problematic section
  11. // Around position 1624 we have: ...]},{"number":3,"title":"干枯的笔",...
  12. // Try to create a minimal JSON with just the section around the error
  13. const before = '{"chapters":[{"writingInstructions":{"mustNotRepeat":["a","b"]},';
  14. const after = '}]}';
  15. const middle = aiResponse.substring(1610, 1700);
  16. const testJson = before + middle + after;
  17. console.log('\n=== Test: surround error area with valid JSON ===');
  18. console.log('Test JSON length:', testJson.length);
  19. try {
  20. const data = JSON.parse(testJson);
  21. console.log('SUCCESS');
  22. } catch(e) {
  23. console.log('FAILED:', e.message.substring(0, 100));
  24. }
  25. // Now let's check: maybe the issue is with the FIRST character after the error position
  26. // Let me see what happens if we parse from 1620 to 1650
  27. console.log('\n=== Parse tiny snippet ===');
  28. const snippet = aiResponse.substring(1600, 1650);
  29. console.log('Snippet:', JSON.stringify(snippet));
  30. try {
  31. JSON.parse(snippet);
  32. console.log('SUCCESS');
  33. } catch(e) {
  34. console.log('FAILED:', e.message);
  35. }
  36. // Try parsing the JSON up to position 1620
  37. console.log('\n=== Parse up to position 1620 ===');
  38. const prefix = aiResponse.substring(0, 1620) + '"]}]}]}';
  39. try {
  40. JSON.parse(prefix);
  41. console.log('SUCCESS');
  42. } catch(e) {
  43. console.log('FAILED:', e.message.substring(0, 100));
  44. }
  45. // Most importantly - let's check if the ACTUAL issue is that the AI returned a malformed
  46. // JSON that's missing some key structure. Let me check the mainTheme and chapters structure
  47. console.log('\n=== Check main structure ===');
  48. const mainThemeMatch = aiResponse.match(/"mainTheme"\s*:\s*"([^"]+)"/);
  49. console.log('mainTheme found:', mainThemeMatch ? 'yes, length ' + mainThemeMatch[1].length : 'no');
  50. const chaptersArrayMatch = aiResponse.match(/"chapters"\s*:\s*\[/);
  51. console.log('chapters array found:', chaptersArrayMatch ? 'yes' : 'no');
  52. const mainThemeValue = mainThemeMatch ? mainThemeMatch[1] : '';
  53. console.log('\nmainTheme value first 100 chars:', JSON.stringify(mainThemeValue.substring(0, 100)));
  54. // Let me try to count braces and brackets in the entire string to find imbalance
  55. console.log('\n=== Brace/Bracket counting ===');
  56. let braceLevel = 0;
  57. let bracketLevel = 0;
  58. let inString = false;
  59. let escapeNext = false;
  60. let minBraceLevel = 0;
  61. let minBracketLevel = 0;
  62. for (let i = 0; i < aiResponse.length; i++) {
  63. const ch = aiResponse[i];
  64. if (escapeNext) { escapeNext = false; continue; }
  65. if (ch === '\\') { escapeNext = true; continue; }
  66. if (ch === '"') { inString = !inString; continue; }
  67. if (inString) continue;
  68. if (ch === '{') { braceLevel++; }
  69. else if (ch === '}') { braceLevel--; if (braceLevel < minBraceLevel) minBraceLevel = braceLevel; }
  70. else if (ch === '[') { bracketLevel++; }
  71. else if (ch === ']') { bracketLevel--; if (bracketLevel < minBracketLevel) minBracketLevel = bracketLevel; }
  72. }
  73. console.log('Final brace level:', braceLevel, '(should be 0)');
  74. console.log('Final bracket level:', bracketLevel, '(should be 0)');
  75. console.log('Minimum brace level reached:', minBraceLevel);
  76. console.log('Minimum bracket level reached:', minBracketLevel);
  77. if (braceLevel !== 0 || bracketLevel !== 0) {
  78. console.log('JSON is UNBALANCED - this is the root cause!');
  79. } else {
  80. console.log('JSON is balanced - the issue must be somewhere specific');
  81. }