controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 || '标准教程';
  26. // 创建书籍(先设置一个预估章节数,实际数量由AI分析后确定)
  27. const scaleToChapters = {
  28. '800': 1,
  29. '2000': 1,
  30. '5000': 1,
  31. 小册子: 5,
  32. 标准教程: 10,
  33. 系统教材: 15,
  34. };
  35. const estimatedChapters = scaleToChapters[bookScale] || 10;
  36. const book = await book_generator_store_1.bookStore.create({
  37. title: body.title,
  38. description: body.description,
  39. totalChapters: estimatedChapters,
  40. });
  41. // 启动 LangGraph 生成(异步,不阻塞)
  42. book_langgraph_1.langGraphGenerator.generate(book.id, body.description, bookScale).catch(err => {
  43. console.error('[LangGraph] 生成失败:', err);
  44. });
  45. ctx.body = {
  46. code: 0,
  47. message: 'LangGraph 生成任务已启动',
  48. data: {
  49. bookId: book.id,
  50. taskId: `lg_${book.id}_${Date.now()}`,
  51. status: 'started',
  52. },
  53. };
  54. }
  55. catch (error) {
  56. console.error('启动失败:', error);
  57. ctx.status = 500;
  58. ctx.body = {
  59. code: 1,
  60. message: error instanceof Error ? error.message : '启动失败',
  61. };
  62. }
  63. });
  64. /**
  65. * POST /api/book-generator/langgraph/books/:id/generate
  66. * 对已有书籍使用 LangGraph 生成
  67. */
  68. router.post('/books/:id/generate', async (ctx) => {
  69. try {
  70. const bookId = ctx.params.id;
  71. const book = await book_generator_store_1.bookStore.getById(bookId);
  72. if (!book) {
  73. ctx.status = 404;
  74. ctx.body = { code: 1, message: '书籍不存在' };
  75. return;
  76. }
  77. // 启动 LangGraph 生成
  78. book_langgraph_1.langGraphGenerator.generate(bookId, book.description, '标准教程' // 固定为标准教程规模
  79. ).catch(err => {
  80. console.error('[LangGraph] 生成失败:', err);
  81. });
  82. ctx.body = {
  83. code: 0,
  84. message: 'LangGraph 生成任务已启动',
  85. data: {
  86. bookId,
  87. taskId: `lg_${bookId}_${Date.now()}`,
  88. status: 'started',
  89. },
  90. };
  91. }
  92. catch (error) {
  93. console.error('启动失败:', error);
  94. ctx.status = 500;
  95. ctx.body = {
  96. code: 1,
  97. message: error instanceof Error ? error.message : '启动失败',
  98. };
  99. }
  100. });
  101. exports.default = router;