regenerate-book.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const http = require('http');
  2. const bookId = 1;
  3. console.log(`开始重新生成书籍 #${bookId} 的大纲...`);
  4. const data = JSON.stringify({
  5. bookScale: '130000'
  6. });
  7. const options = {
  8. hostname: 'localhost',
  9. port: 3000,
  10. path: `/api/book-generator/langgraph/books/${bookId}/generate`,
  11. method: 'POST',
  12. headers: {
  13. 'Content-Type': 'application/json',
  14. 'Content-Length': Buffer.byteLength(data)
  15. }
  16. };
  17. function makeRequest(retryCount = 0) {
  18. const req = http.request(options, (res) => {
  19. let body = '';
  20. res.on('data', (chunk) => {
  21. body += chunk;
  22. });
  23. res.on('end', () => {
  24. if (res.statusCode === 429 && retryCount < 3) {
  25. const retryAfter = res.headers['retry-after'] || 2;
  26. console.log(`⚠️ 请求过于频繁,${retryAfter}秒后重试... (第${retryCount + 1}次)`);
  27. setTimeout(() => makeRequest(retryCount + 1), retryAfter * 1000);
  28. return;
  29. }
  30. console.log('响应状态:', res.statusCode);
  31. try {
  32. const responseData = JSON.parse(body);
  33. console.log('响应数据:', responseData);
  34. if (res.statusCode === 200) {
  35. console.log('\n✅ 生成任务已启动!');
  36. console.log('请等待几分钟,生成过程包括:');
  37. console.log(' 1. 生成一级大纲(章)');
  38. console.log(' 2. 生成二级大纲(节)');
  39. console.log(' 3. 生成三级大纲(小节)');
  40. console.log(' 4. 生成章节内容');
  41. console.log(' 5. 生成前言和后记');
  42. console.log('\n你可以通过以下方式查看进度:');
  43. console.log(` - 访问: http://localhost:5173/#/pages/book-generator/index?id=${bookId}`);
  44. console.log(' - 或查看服务器日志');
  45. } else {
  46. console.log('\n❌ 请求失败:', responseData.message);
  47. }
  48. } catch (e) {
  49. console.log('响应体:', body);
  50. }
  51. });
  52. });
  53. req.on('error', (e) => {
  54. console.error('请求失败:', e.message);
  55. });
  56. req.write(data);
  57. req.end();
  58. }
  59. makeRequest();