add-subsection-checkpoint.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 oldCode = ` // 从数据库获取所有小节(level=3)
  6. const subsections = await prisma.bookChapter.findMany({
  7. where: { bookId: bookIdNum, level: 3 },
  8. orderBy: [
  9. { parentId: 'asc' }, // 先按父节点排序
  10. { number: 'asc' }
  11. ],
  12. include: {
  13. // 获取父节的信息
  14. parent: {
  15. include: {
  16. // 获取父章的信息
  17. parent: true
  18. }
  19. }
  20. }
  21. });
  22. if (subsections.length === 0) {
  23. console.log('[LangGraph] 没有小节,跳过内容生成');
  24. return { finished: true, progress: 90 };
  25. }`;
  26. const newCode = ` // 从数据库获取所有小节(level=3)
  27. const allSubsections = await prisma.bookChapter.findMany({
  28. where: { bookId: bookIdNum, level: 3 },
  29. orderBy: [
  30. { parentId: 'asc' }, // 先按父节点排序
  31. { number: 'asc' }
  32. ],
  33. include: {
  34. // 获取父节的信息
  35. parent: {
  36. include: {
  37. // 获取父章的信息
  38. parent: true
  39. }
  40. }
  41. }
  42. });
  43. if (allSubsections.length === 0) {
  44. console.log('[LangGraph] 没有小节,跳过内容生成');
  45. return { finished: true, progress: 90 };
  46. }
  47. // 筛选出未完成的小节(pending 或 failed),支持断点续传
  48. const subsections = allSubsections.filter(s => s.status !== 'completed');
  49. const completedCount = allSubsections.length - subsections.length;
  50. if (completedCount > 0) {
  51. console.log(\`[LangGraph] ✅ 跳过 \${completedCount} 个已完成的小节,待生成 \${subsections.length} 个\`);
  52. }
  53. if (subsections.length === 0) {
  54. console.log('[LangGraph] ✅ 所有小节已完成');
  55. return { finished: true, progress: 95 };
  56. }`;
  57. content = content.replace(oldCode, newCode);
  58. // 修改进度计算:从已完成的数量开始
  59. content = content.replace(
  60. /const totalSubsections = subsections\.length;\s+let completedSubsections = 0;/,
  61. `const totalSubsections = allSubsections.length;
  62. let completedSubsections = completedCount; // 从已完成的数量开始`
  63. );
  64. fs.writeFileSync(file, content, 'utf8');
  65. console.log('✅ 断点续传逻辑已添加');