| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- "use strict";
- /**
- * 书籍生成控制器 - API 路由
- */
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const router_1 = __importDefault(require("@koa/router"));
- const book_generator_service_1 = require("./book-generator.service");
- const book_generator_store_1 = require("./book-generator.store");
- const book_generator_workflow_1 = require("./book-generator.workflow");
- const router = new router_1.default();
- /**
- * POST /book-generator/books
- * 创建新书籍
- */
- router.post('/books', async (ctx) => {
- try {
- const request = ctx.request.body;
- if (!request.title || !request.description) {
- ctx.status = 400;
- ctx.body = {
- code: 1,
- message: '书名和描述不能为空',
- };
- return;
- }
- const book = await book_generator_service_1.bookGeneratorService.createBook(request);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { book },
- };
- }
- catch (error) {
- console.error('创建书籍失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '创建失败',
- };
- }
- });
- /**
- * GET /api/book-generator/books
- * 获取所有书籍列表
- */
- router.get('/books', async (ctx) => {
- try {
- const books = await book_generator_store_1.bookStore.getAllByUser();
- ctx.body = {
- code: 0,
- message: 'success',
- data: {
- books: books.map((b) => ({
- id: b.id,
- title: b.title,
- subtitle: b.subtitle,
- description: b.description,
- status: b.status,
- progress: b.progress,
- totalChapters: b.totalChapters,
- completedChapters: b.chapters.filter((c) => c.status === 'completed').length,
- createdAt: b.createdAt,
- })),
- },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取失败',
- };
- }
- });
- /**
- * GET /api/book-generator/books/:id
- * 获取书籍详情
- */
- router.get('/books/:id', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const book = await book_generator_store_1.bookStore.getById(bookId);
- if (!book) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '书籍不存在',
- };
- return;
- }
- ctx.body = {
- code: 0,
- message: 'success',
- data: { book },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取失败',
- };
- }
- });
- /**
- * DELETE /api/book-generator/books/:id
- * 删除书籍
- */
- router.delete('/books/:id', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const deleted = await book_generator_store_1.bookStore.delete(bookId);
- if (!deleted) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '书籍不存在',
- };
- return;
- }
- ctx.body = {
- code: 0,
- message: '删除成功',
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '删除失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/outline
- * 生成大纲
- */
- router.post('/books/:id/outline', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const outline = await book_generator_service_1.bookGeneratorService.generateOutline(bookId);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { outline },
- };
- }
- catch (error) {
- console.error('生成大纲失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成大纲失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/chapters
- * 生成章节
- */
- router.post('/books/:id/chapters', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const body = ctx.request.body;
- if (body.chapterNumber) {
- // 生成单个章节
- const chapter = await book_generator_service_1.bookGeneratorService.generateChapter(bookId, body.chapterNumber);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { chapter },
- };
- }
- else {
- // 生成全部章节
- const chapters = await book_generator_service_1.bookGeneratorService.generateAllChapters(bookId);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { chapters },
- };
- }
- }
- catch (error) {
- console.error('生成章节失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成章节失败',
- };
- }
- });
- /**
- * GET /api/book-generator/books/:id/chapters/:chapterNumber
- * 获取指定章节
- */
- router.get('/books/:id/chapters/:chapterNumber', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const book = await book_generator_store_1.bookStore.getById(bookId);
- if (!book) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '书籍不存在',
- };
- return;
- }
- const chapterNumber = parseInt(ctx.params.chapterNumber);
- const chapter = book.chapters.find((c) => c.number === chapterNumber);
- if (!chapter) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '章节不存在',
- };
- return;
- }
- ctx.body = {
- code: 0,
- message: 'success',
- data: { chapter },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取章节失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/foreword
- * 生成前言
- */
- router.post('/books/:id/foreword', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const foreword = await book_generator_service_1.bookGeneratorService.generateForeword(bookId);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { foreword },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成前言失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/afterword
- * 生成后记
- */
- router.post('/books/:id/afterword', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const afterword = await book_generator_service_1.bookGeneratorService.generateAfterword(bookId);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { afterword },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成后记失败',
- };
- }
- });
- /**
- * GET /api/book-generator/books/:id/full-content
- * 获取完整书籍内容
- */
- router.get('/books/:id/full-content', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const content = await book_generator_service_1.bookGeneratorService.getFullContent(bookId);
- ctx.body = {
- code: 0,
- message: 'success',
- data: { content },
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取内容失败',
- };
- }
- });
- /**
- * GET /api/book-generator/books/:id/progress
- * 获取生成进度
- */
- router.get('/books/:id/progress', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const progress = await book_generator_service_1.bookGeneratorService.getProgress(bookId);
- if (!progress) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '书籍不存在',
- };
- return;
- }
- ctx.body = {
- code: 0,
- message: 'success',
- data: progress,
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取进度失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/generate
- * 一键生成整本书(异步,后端立即返回任务ID,前端轮询进度)
- */
- router.post('/books/:id/generate', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const body = ctx.request.body;
- // 立即返回任务ID,不等待生成完成
- const taskId = `task_${bookId}_${Date.now()}`;
- // 启动异步生成任务(不阻塞,立即返回)
- book_generator_service_1.bookGeneratorService.generateBookAsync(bookId, {
- generateForeword: body.generateForeword ?? true,
- generateAfterword: body.generateAfterword ?? true,
- }).catch(err => {
- console.error('异步生成失败:', err);
- });
- ctx.body = {
- code: 0,
- message: '生成任务已启动',
- data: {
- taskId,
- bookId,
- status: 'started',
- },
- };
- }
- catch (error) {
- console.error('启动生成失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '启动失败',
- };
- }
- });
- /**
- * GET /api/book-generator/workflow/:bookId
- * 获取工作流状态
- */
- router.get('/workflow/:bookId', (ctx) => {
- try {
- const bookId = ctx.params.bookId;
- const progress = book_generator_workflow_1.workflowEngine.getProgress(bookId);
- if (!progress) {
- ctx.status = 404;
- ctx.body = {
- code: 1,
- message: '工作流不存在',
- };
- return;
- }
- ctx.body = {
- code: 0,
- message: 'success',
- data: progress,
- };
- }
- catch (error) {
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '获取工作流状态失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/chapters/:chapterNumber/audio
- * 生成单个章节音频
- */
- router.post('/books/:id/chapters/:chapterNumber/audio', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const chapterNumber = parseInt(ctx.params.chapterNumber);
- const { voiceId } = ctx.request.body;
- const result = await book_generator_service_1.bookGeneratorService.generateChapterAudio(bookId, chapterNumber, voiceId || 'cherry');
- ctx.body = {
- code: 0,
- message: '音频生成任务已启动',
- data: result,
- };
- }
- catch (error) {
- console.error('生成章节音频失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成音频失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/audio
- * 批量生成书籍所有章节音频
- */
- router.post('/books/:id/audio', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const { voiceId } = ctx.request.body;
- const result = await book_generator_service_1.bookGeneratorService.generateAllChaptersAudio(bookId, voiceId || 'cherry');
- ctx.body = {
- code: 0,
- message: '批量音频生成任务已启动',
- data: result,
- };
- }
- catch (error) {
- console.error('批量生成音频失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '批量生成音频失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/chapters/:chapterNumber/video
- * 生成单个章节视频(从音频转视频)
- */
- router.post('/books/:id/chapters/:chapterNumber/video', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const chapterNumber = parseInt(ctx.params.chapterNumber);
- const result = await book_generator_service_1.bookGeneratorService.generateChapterVideo(bookId, chapterNumber);
- ctx.body = {
- code: 0,
- message: '视频生成任务已启动',
- data: result,
- };
- }
- catch (error) {
- console.error('生成章节视频失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '生成视频失败',
- };
- }
- });
- /**
- * POST /api/book-generator/books/:id/videos
- * 批量生成书籍所有章节视频
- */
- router.post('/books/:id/videos', async (ctx) => {
- try {
- const bookId = ctx.params.id;
- const result = await book_generator_service_1.bookGeneratorService.generateAllChaptersVideo(bookId);
- ctx.body = {
- code: 0,
- message: '批量视频生成任务已启动',
- data: result,
- };
- }
- catch (error) {
- console.error('批量生成视频失败:', error);
- ctx.status = 500;
- ctx.body = {
- code: 1,
- message: error instanceof Error ? error.message : '批量生成视频失败',
- };
- }
- });
- exports.default = router;
|