rewrite-parser.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const fs = require('fs');
  2. const file = 'src/modules/book-generator/langgraph-generator.ts';
  3. let content = fs.readFileSync(file, 'utf8');
  4. // 找到 parseSubsections 函数并完全替换
  5. const startMarker = 'function parseSubsections(jsonStr: string): { subsections: any[] } | null {';
  6. const endMarker = '// ============ LangGraph 节点 ============';
  7. const startIndex = content.indexOf(startMarker);
  8. const endIndex = content.indexOf(endMarker);
  9. if (startIndex === -1 || endIndex === -1) {
  10. console.error('未找到函数标记');
  11. process.exit(1);
  12. }
  13. const newFunction = `function parseSubsections(jsonStr: string): { subsections: any[] } | null {
  14. if (!jsonStr || typeof jsonStr !== 'string') {
  15. console.error('[SubsectionParser] 输入为空或非字符串');
  16. return null;
  17. }
  18. try {
  19. let data: any;
  20. let cleanedStr = jsonStr.trim();
  21. // 移除 <think>...</think> 标签
  22. const thinkMatch = cleanedStr.match(/</think>([\\s\\S]*)/);
  23. if (thinkMatch) {
  24. cleanedStr = thinkMatch[1].trim();
  25. }
  26. // 移除 markdown 代码块标记
  27. cleanedStr = cleanedStr.replace(/^\`\`\`json\\s*/gi, '').replace(/^\`\`\`\\s*/gm, '').replace(/\`\`\`\\s*$/gm, '').trim();
  28. // 找到第一个 { 和最后一个 }
  29. const firstBrace = cleanedStr.indexOf('{');
  30. const lastBrace = cleanedStr.lastIndexOf('}');
  31. if (firstBrace === -1 || lastBrace === -1) {
  32. console.error('[SubsectionParser] 未找到 JSON 对象');
  33. return null;
  34. }
  35. const jsonStr_extracted = cleanedStr.substring(firstBrace, lastBrace + 1);
  36. try {
  37. data = JSON.parse(jsonStr_extracted);
  38. } catch (parseErr) {
  39. console.error('[SubsectionParser] JSON 解析失败:', parseErr);
  40. console.error('[SubsectionParser] 提取的JSON前100字符:', jsonStr_extracted.substring(0, 100));
  41. return null;
  42. }
  43. if (!data || typeof data !== 'object') {
  44. console.error('[SubsectionParser] 解析结果非对象');
  45. return null;
  46. }
  47. // 兼容:如果顶层就是数组
  48. if (Array.isArray(data)) {
  49. data = { subsections: data };
  50. }
  51. // 兼容多种字段名:subsections, Subsections, subsection_list 等
  52. let subsectionsArray = data.subsections || data.Subsections || data.subsection_list || data.section_subsections;
  53. if (!Array.isArray(subsectionsArray)) {
  54. console.error('[SubsectionParser] subsections 字段缺失或非数组');
  55. return null;
  56. }
  57. return {
  58. subsections: subsectionsArray.map((s: any, i: number) => ({
  59. number: s.number || i + 1,
  60. title: s.title || \`第\${i + 1}小节\`,
  61. summary: typeof s.summary === 'string' ? s.summary : '',
  62. keyPoints: Array.isArray(s.keyPoints) ? s.keyPoints : [],
  63. estimatedWords: typeof s.estimatedWords === 'number' ? s.estimatedWords : 500,
  64. })),
  65. };
  66. } catch (err) {
  67. console.error('[SubsectionParser] 未知错误:', err);
  68. return null;
  69. }
  70. }
  71. `;
  72. content = content.substring(0, startIndex) + newFunction + content.substring(endIndex);
  73. fs.writeFileSync(file, content, 'utf8');
  74. console.log('✅ parseSubsections 函数已重写');