debug-parse13.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // My fix function
  11. function fixMissingQuotes(jsonStr) {
  12. return jsonStr
  13. .replace(/\{number":/g, '{"number":')
  14. .replace(/\{title":/g, '{"title":')
  15. .replace(/\{summary":/g, '{"summary":')
  16. .replace(/\{opening":/g, '{"opening":')
  17. .replace(/\{structure":/g, '{"structure":')
  18. .replace(/\{mustCover":/g, '{"mustCover":')
  19. .replace(/\{mustNotRepeat":/g, '{"mustNotRepeat":')
  20. .replace(/\{toneAdjustment":/g, '{"toneAdjustment":')
  21. .replace(/\{keyTakeaway":/g, '{"keyTakeaway":')
  22. .replace(/\{keyPoints":/g, '{"keyPoints":')
  23. .replace(/\{estimatedWords":/g, '{"estimatedWords":')
  24. .replace(/\{writingInstructions":/g, '{"writingInstructions":');
  25. }
  26. // Direct replace test
  27. const directFixed = fixMissingQuotes(aiResponse);
  28. console.log('\nDirect fix - chars changed:', aiResponse.length - directFixed.split('').filter((c,i) => c !== directFixed[i]).length);
  29. // Test direct fix
  30. try {
  31. const data = JSON.parse(directFixed);
  32. console.log('Direct fix: SUCCESS, chapters:', data.chapters?.length);
  33. } catch(e) {
  34. console.log('Direct fix: FAILED -', e.message.substring(0, 80));
  35. // Check if there's actually a replacement happening
  36. const badPattern = /\{number":/g;
  37. const count = (aiResponse.match(badPattern) || []).length;
  38. console.log('Bad pattern "{number\\": found:', count, 'times in original');
  39. // Try a broader fix approach - replace ,{word": with ,{"word":
  40. const broaderFixed = aiResponse.replace(/,(\{[^"]+":)/g, function(m, p1) {
  41. return ',{"' + p1.slice(1);
  42. });
  43. console.log('Broader fix - chars changed:', aiResponse.length - broaderFixed.split('').filter((c,i) => c !== broaderFixed[i]).length);
  44. try {
  45. const data = JSON.parse(broaderFixed);
  46. console.log('Broader fix: SUCCESS, chapters:', data.chapters?.length);
  47. } catch(e2) {
  48. console.log('Broader fix: FAILED -', e2.message.substring(0, 80));
  49. // Check the exact position
  50. const pos = parseInt(e2.message.match(/position (\d+)/)?.[1] || '0');
  51. console.log('Error at position:', pos);
  52. console.log('Context:', JSON.stringify(aiResponse.substring(Math.max(0,pos-50), pos+50)));
  53. }
  54. }
  55. // Also test: what if the problem is not missing quotes but something else?
  56. // Let's check if there are any single quotes being used instead of double quotes
  57. const singleQuoteProps = /'[^']*':/g;
  58. const singleQuoteMatches = aiResponse.match(singleQuoteProps);
  59. console.log('\nSingle-quoted properties found:', singleQuoteMatches ? singleQuoteMatches.length : 0);
  60. if (singleQuoteMatches && singleQuoteMatches.length < 10) {
  61. singleQuoteMatches.forEach(m => console.log(' ', m));
  62. }