add-chapter-checkpoint.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 = ` // 获取当前已生成的字数(用于断点续传)
  6. let currentWordCount = await getGeneratedWordCount(state.bookId);
  7. console.log(\`[LangGraph] 当前已生成字数: \${currentWordCount}\`);
  8. for (const chapterOutline of book.outline.chapters) {`;
  9. const newCode = ` // 获取当前已生成的字数(用于断点续传)
  10. let currentWordCount = await getGeneratedWordCount(state.bookId);
  11. console.log(\`[LangGraph] 当前已生成字数: \${currentWordCount}\`);
  12. // 查询已完成的章节,支持断点续传
  13. const completedChapterNumbers = new Set<number>();
  14. for (const ch of book.outline.chapters) {
  15. const chapter = await bookStore.getChapter(state.bookId, ch.number);
  16. if (chapter && chapter.status === 'completed') {
  17. completedChapterNumbers.add(ch.number);
  18. }
  19. }
  20. if (completedChapterNumbers.size > 0) {
  21. console.log(\`[LangGraph] ✅ 跳过 \${completedChapterNumbers.size} 个已完成的章节: [\${Array.from(completedChapterNumbers).join(', ')}]\`);
  22. }
  23. for (const chapterOutline of book.outline.chapters) {
  24. // 跳过已完成的章节
  25. if (completedChapterNumbers.has(chapterOutline.number)) {
  26. console.log(\`[LangGraph] 跳过第\${chapterOutline.number}章(已完成)\`);
  27. continue;
  28. }`;
  29. content = content.replace(oldCode, newCode);
  30. fs.writeFileSync(file, content, 'utf8');
  31. console.log('✅ 断点续传已添加');