debug-parse5.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const fs = require('fs');
  2. const content = fs.readFileSync('temp/rich-outline-fail_57_1779014691106.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. const pos = 523;
  11. console.log('\n=== Position', pos, 'context ===');
  12. console.log(JSON.stringify(aiResponse.substring(Math.max(0,pos-80), pos+80)));
  13. // Check what char is at position 523
  14. console.log('\nChar at 523:', JSON.stringify(aiResponse[523]));
  15. console.log('Char code at 523:', aiResponse.charCodeAt(523));
  16. // Check if position 523 is inside a string
  17. let inString = false;
  18. let escapeNext = false;
  19. let lastQuotePos = -1;
  20. for (let i = 0; i < pos; i++) {
  21. const ch = aiResponse[i];
  22. if (escapeNext) { escapeNext = false; continue; }
  23. if (ch === '\\') { escapeNext = true; continue; }
  24. if (ch === '"') { inString = !inString; lastQuotePos = i; }
  25. }
  26. console.log('At pos 523, inString:', inString, 'lastQuotePos:', lastQuotePos);
  27. // First 200 chars
  28. console.log('\n=== First 200 chars ===');
  29. console.log(JSON.stringify(aiResponse.substring(0, 200)));
  30. // Check for <think>
  31. console.log('\nContains <think>:', aiResponse.includes('<think>'));
  32. console.log('Starts with {:', aiResponse.trim().startsWith('{'));
  33. // Try the fixed think tag regex
  34. const thinkTagMatch = aiResponse.match(/^<think>[\s\S]*?<\/think>\s*/i);
  35. console.log('thinkTagMatch found:', !!thinkTagMatch);
  36. // Try strategy 1: direct parse
  37. try {
  38. const data = JSON.parse(aiResponse);
  39. console.log('Direct parse SUCCESS, chapters:', data.chapters?.length);
  40. } catch(e) {
  41. console.log('Direct parse failed:', e.message.substring(0, 80));
  42. }
  43. // Try strategy 2: remove code blocks
  44. let cleaned = aiResponse.replace(/```json\s*/g, '').replace(/```\s*/g, '');
  45. try {
  46. const data = JSON.parse(cleaned);
  47. console.log('After codeblock removal SUCCESS, chapters:', data.chapters?.length);
  48. } catch(e) {
  49. console.log('Strategy 2 failed:', e.message.substring(0, 80));
  50. }
  51. // Strategy 3: regex extract
  52. const match = aiResponse.match(/\{[\s\S]*\}/);
  53. if (match) {
  54. try {
  55. const data = JSON.parse(match[0]);
  56. console.log('Strategy 3 SUCCESS, chapters:', data.chapters?.length);
  57. } catch(e) {
  58. console.log('Strategy 3 failed:', e.message.substring(0, 80));
  59. }
  60. } else {
  61. console.log('Strategy 3: no JSON object found');
  62. }