"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;