fix-checkpoint.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const fs = require('fs');
  2. const file = 'src/modules/book-generator/langgraph-generator.ts';
  3. let content = fs.readFileSync(file, 'utf8');
  4. // 找到并替换整个小节查询部分
  5. const startMarker = ' // 从数据库获取所有小节(level=3)';
  6. const endMarker = ' // 构建父节点映射,便于获取上下文';
  7. const startIdx = content.indexOf(startMarker);
  8. const endIdx = content.indexOf(endMarker);
  9. if (startIdx === -1 || endIdx === -1) {
  10. console.log('❌ 未找到标记');
  11. process.exit(1);
  12. }
  13. const newCode = ` // 从数据库获取所有小节(level=3)
  14. const allSubsections = await prisma.bookChapter.findMany({
  15. where: { bookId: bookIdNum, level: 3 },
  16. orderBy: [
  17. { parentId: 'asc' }, // 先按父节点排序
  18. { number: 'asc' }
  19. ],
  20. include: {
  21. // 获取父节的信息
  22. parent: {
  23. include: {
  24. // 获取父章的信息
  25. parent: true
  26. }
  27. }
  28. }
  29. });
  30. if (allSubsections.length === 0) {
  31. console.log('[LangGraph] 没有小节,跳过内容生成');
  32. return { finished: true, progress: 90 };
  33. }
  34. // 筛选出未完成的小节(pending 或 failed),支持断点续传
  35. const subsections = allSubsections.filter(s => s.status !== 'completed');
  36. const completedCount = allSubsections.length - subsections.length;
  37. if (completedCount > 0) {
  38. console.log(\`[LangGraph] ✅ 跳过 \${completedCount} 个已完成的小节,待生成 \${subsections.length} 个\`);
  39. }
  40. if (subsections.length === 0) {
  41. console.log('[LangGraph] ✅ 所有小节已完成');
  42. return { finished: true, progress: 95 };
  43. }
  44. `;
  45. content = content.slice(0, startIdx) + newCode + content.slice(endIdx);
  46. // 修改进度计算
  47. content = content.replace(
  48. /const totalSubsections = subsections\.length;\s+let completedSubsections = 0;/,
  49. `const totalSubsections = allSubsections.length;
  50. let completedSubsections = completedCount; // 从已完成的数量开始`
  51. );
  52. fs.writeFileSync(file, content, 'utf8');
  53. console.log('✅ 断点续传支持已添加');