debug-parse8.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Check for all missing opening quotes on property names
  10. // Pattern: ,{word": should be ,{"word":
  11. // Common property names that might be affected
  12. const badPatterns = [
  13. /,\{number":/g,
  14. /,\{title":/g,
  15. /,\{summary":/g,
  16. /,\{opening":/g,
  17. /,\{structure":/g,
  18. /,\{mustCover":/g,
  19. /,\{mustNotRepeat":/g,
  20. /,\{toneAdjustment":/g,
  21. /,\{keyTakeaway":/g,
  22. /,\{keyPoints":/g,
  23. /,\{estimatedWords":/g,
  24. /,\{writingInstructions":/g,
  25. ];
  26. for (const pattern of badPatterns) {
  27. const matches = aiResponse.match(pattern);
  28. if (matches) {
  29. console.log(pattern.toString(), '-> found', matches.length, 'times');
  30. }
  31. }
  32. // Can we fix by adding the missing quote?
  33. // The fix would be: ,{word": -> ,{"word":
  34. let fixed = aiResponse;
  35. // Apply fixes for common patterns
  36. fixed = fixed.replace(/,(\{[^"]+":)/g, ',{"'.slice(1) + '$1'.slice(1));
  37. // Wait that's confusing, let me do it simpler
  38. fixed = fixed.replace(/\{number":/g, '{"number":');
  39. fixed = fixed.replace(/\{title":/g, '{"title":');
  40. fixed = fixed.replace(/\{summary":/g, '{"summary":');
  41. fixed = fixed.replace(/\{opening":/g, '{"opening":');
  42. fixed = fixed.replace(/\{structure":/g, '{"structure":');
  43. fixed = fixed.replace(/\{mustCover":/g, '{"mustCover":');
  44. fixed = fixed.replace(/\{mustNotRepeat":/g, '{"mustNotRepeat":');
  45. fixed = fixed.replace(/\{toneAdjustment":/g, '{"toneAdjustment":');
  46. fixed = fixed.replace(/\{keyTakeaway":/g, '{"keyTakeaway":');
  47. fixed = fixed.replace(/\{keyPoints":/g, '{"keyPoints":');
  48. fixed = fixed.replace(/\{estimatedWords":/g, '{"estimatedWords":');
  49. fixed = fixed.replace(/\{writingInstructions":/g, '{"writingInstructions":');
  50. try {
  51. const data = JSON.parse(fixed);
  52. console.log('\nAfter fix: Valid JSON! chapters:', data.chapters?.length);
  53. } catch(e) {
  54. console.log('\nAfter fix: Still invalid:', e.message.substring(0, 100));
  55. // Check remaining issues
  56. let inString = false;
  57. let escapeNext = false;
  58. let i = 0;
  59. const str = fixed;
  60. while (i < str.length) {
  61. const ch = str[i];
  62. if (escapeNext) { escapeNext = false; i++; continue; }
  63. if (ch === '\\') { escapeNext = true; i++; continue; }
  64. if (ch === '"') { inString = !inString; i++; continue; }
  65. if (!inString && ch === '{') {
  66. // Check if next non-space char is a quote
  67. let j = i + 1;
  68. while (j < str.length && str[j] === ' ') j++;
  69. if (str[j] && str[j] !== '"') {
  70. console.log('Missing quote before property at position', i, '->', JSON.stringify(str.substring(i, i+20)));
  71. break;
  72. }
  73. }
  74. i++;
  75. }
  76. }