controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. /**
  3. * LangGraph 书籍生成 - API 路由
  4. */
  5. var __importDefault = (this && this.__importDefault) || function (mod) {
  6. return (mod && mod.__esModule) ? mod : { "default": mod };
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. const router_1 = __importDefault(require("@koa/router"));
  10. const book_langgraph_1 = require("./book-langgraph");
  11. const book_generator_store_1 = require("../book-generator.store");
  12. const router = new router_1.default();
  13. /**
  14. * POST /api/book-generator/langgraph/books
  15. * 使用 LangGraph 创建并生成书籍
  16. */
  17. router.post('/books', async (ctx) => {
  18. try {
  19. const body = ctx.request.body;
  20. if (!body.title || !body.description) {
  21. ctx.status = 400;
  22. ctx.body = { code: 1, message: '书名和描述不能为空' };
  23. return;
  24. }
  25. const bookScale = body.bookScale || '130000';
  26. // 创建书籍(先设置一个预估章节数,实际数量由AI分析后确定)
  27. const scaleToChapters = {
  28. '800': 1,
  29. '2000': 1,
  30. '5000': 1,
  31. '130000': 10,
  32. '340000': 15,
  33. };
  34. const estimatedChapters = scaleToChapters[bookScale] || 10;
  35. const book = await book_generator_store_1.bookStore.create({
  36. title: body.title,
  37. description: body.description,
  38. totalChapters: estimatedChapters,
  39. });
  40. // 启动 LangGraph 生成(异步,不阻塞)
  41. book_langgraph_1.langGraphGenerator.generate(book.id, body.description, bookScale).catch(err => {
  42. console.error('[LangGraph] 生成失败:', err);
  43. });
  44. ctx.body = {
  45. code: 0,
  46. message: 'LangGraph 生成任务已启动',
  47. data: {
  48. bookId: book.id,
  49. taskId: `lg_${book.id}_${Date.now()}`,
  50. status: 'started',
  51. },
  52. };
  53. }
  54. catch (error) {
  55. console.error('启动失败:', error);
  56. ctx.status = 500;
  57. ctx.body = {
  58. code: 1,
  59. message: error instanceof Error ? error.message : '启动失败',
  60. };
  61. }
  62. });
  63. /**
  64. * POST /api/book-generator/langgraph/books/:id/generate
  65. * 对已有书籍使用 LangGraph 生成
  66. */
  67. router.post('/books/:id/generate', async (ctx) => {
  68. try {
  69. const bookId = ctx.params.id;
  70. const book = await book_generator_store_1.bookStore.getById(bookId);
  71. if (!book) {
  72. ctx.status = 404;
  73. ctx.body = { code: 1, message: '书籍不存在' };
  74. return;
  75. }
  76. // 启动 LangGraph 生成
  77. book_langgraph_1.langGraphGenerator.generate(bookId, book.description, '130000'
  78. ).catch(err => {
  79. console.error('[LangGraph] 生成失败:', err);
  80. });
  81. ctx.body = {
  82. code: 0,
  83. message: 'LangGraph 生成任务已启动',
  84. data: {
  85. bookId,
  86. taskId: `lg_${bookId}_${Date.now()}`,
  87. status: 'started',
  88. },
  89. };
  90. }
  91. catch (error) {
  92. console.error('启动失败:', error);
  93. ctx.status = 500;
  94. ctx.body = {
  95. code: 1,
  96. message: error instanceof Error ? error.message : '启动失败',
  97. };
  98. }
  99. });
  100. exports.default = router;