create-mini-book.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { PrismaClient } = require('@prisma/client');
  2. const http = require('http');
  3. async function createMiniBook() {
  4. console.log('创建迷你测试书籍(2章)...\n');
  5. // 1. 创建书籍
  6. const bookData = {
  7. title: 'Python入门教程',
  8. description: '面向初学者的Python编程基础教程',
  9. bookScale: '小册子', // 最小规模:2-5万字,2-5章
  10. targetAudience: '编程初学者',
  11. knowledgeDepth: '入门',
  12. industry: '教育',
  13. writingStyle: '通俗易懂',
  14. };
  15. const postData = JSON.stringify(bookData);
  16. const options = {
  17. hostname: 'localhost',
  18. port: 3000,
  19. path: '/api/book-generator/langgraph/books',
  20. method: 'POST',
  21. headers: {
  22. 'Content-Type': 'application/json',
  23. 'Content-Length': Buffer.byteLength(postData),
  24. },
  25. };
  26. const response = await new Promise((resolve, reject) => {
  27. const req = http.request(options, (res) => {
  28. let data = '';
  29. res.on('data', chunk => data += chunk);
  30. res.on('end', () => {
  31. resolve({
  32. statusCode: res.statusCode,
  33. body: JSON.parse(data),
  34. });
  35. });
  36. });
  37. req.on('error', reject);
  38. req.write(postData);
  39. req.end();
  40. });
  41. console.log('书籍创建响应:');
  42. console.log(JSON.stringify(response.body, null, 2));
  43. if (response.body.code === 0) {
  44. const bookId = parseInt(response.body.data.book.id);
  45. console.log(`\n✅ 书籍创建成功!bookId=${bookId}`);
  46. console.log(`\n访问前端页面:http://localhost:5173/#/pages/book-generator/index?id=${bookId}`);
  47. console.log('\n开始监控生成进度...\n');
  48. // 2. 监控生成进度
  49. const prisma = new PrismaClient();
  50. for (let i = 0; i < 30; i++) { // 最多监控30次,每次10秒
  51. await new Promise(resolve => setTimeout(resolve, 10000)); // 等待10秒
  52. const book = await prisma.book.findUnique({
  53. where: { id: bookId },
  54. select: {
  55. id: true,
  56. status: true,
  57. progress: true,
  58. errorMsg: true,
  59. }
  60. });
  61. const chapters = await prisma.bookChapter.count({ where: { bookId, level: 1 } });
  62. const sections = await prisma.bookChapter.count({ where: { bookId, level: 2 } });
  63. const subsections = await prisma.bookChapter.count({ where: { bookId, level: 3 } });
  64. const completedContent = await prisma.bookChapter.count({
  65. where: { bookId, level: 3, contentStatus: 'completed' }
  66. });
  67. console.log(`[${new Date().toLocaleTimeString()}] 状态: ${book.status}, 进度: ${book.progress}%, ` +
  68. `章: ${chapters}, 节: ${sections}, 小节: ${subsections}, 已完成内容: ${completedContent}`);
  69. if (book.errorMsg) {
  70. console.log(` ❌ 错误: ${book.errorMsg}`);
  71. break;
  72. }
  73. if (book.status === 'completed') {
  74. console.log('\n🎉 生成完成!');
  75. // 显示生成的内容
  76. const firstContent = await prisma.bookChapter.findFirst({
  77. where: { bookId, level: 3, contentStatus: 'completed' },
  78. select: { title: true, content: true, wordCount: true }
  79. });
  80. if (firstContent) {
  81. console.log(`\n📝 第一个完成的小节: ${firstContent.title}`);
  82. console.log(`字数: ${firstContent.wordCount}`);
  83. console.log(`内容预览:\n${firstContent.content.substring(0, 500)}...`);
  84. }
  85. break;
  86. }
  87. }
  88. await prisma.$disconnect();
  89. }
  90. }
  91. createMiniBook().catch(console.error);