"use strict";
/**
 * 书籍生成工作流引擎
 * 参考 LangGraph 思路：状态机 + 条件边 + 节点执行
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.workflowEngine = void 0;
const book_generator_service_1 = require("./book-generator.service");
// ============ 工作流引擎 ============
class BookWorkflowEngine {
    workflows = new Map();
    /**
     * 启动工作流
     */
    startWorkflow(bookId, totalChapters) {
        const state = {
            bookId,
            currentNode: 'idle',
            currentChapter: 0,
            totalChapters,
            completedChapters: 0,
            pendingChapters: Array.from({ length: totalChapters }, (_, i) => i + 1),
            failedChapters: [],
            phase: 'planning',
            startedAt: new Date(),
            updatedAt: new Date(),
        };
        this.workflows.set(bookId, state);
        return state;
    }
    /**
     * 获取工作流状态
     */
    getState(bookId) {
        return this.workflows.get(bookId);
    }
    /**
     * 更新工作流状态
     */
    updateState(bookId, updates) {
        const state = this.workflows.get(bookId);
        if (!state)
            return undefined;
        Object.assign(state, updates, { updatedAt: new Date() });
        return state;
    }
    /**
     * 执行规划阶段
     */
    async executePlanningPhase(bookId) {
        const state = this.workflows.get(bookId);
        if (!state)
            return { success: false, error: '工作流不存在' };
        this.updateState(bookId, { currentNode: 'planning', phase: 'planning' });
        try {
            const outline = await book_generator_service_1.bookGeneratorService.generateOutline(bookId);
            // 更新工作流状态
            this.updateState(bookId, {
                currentNode: 'idle',
                phase: 'writing',
                pendingChapters: outline.chapters.map(c => c.number),
            });
            return { success: true, outline };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : '规划失败';
            this.updateState(bookId, { currentNode: 'failed', error: errorMessage });
            return { success: false, error: errorMessage };
        }
    }
    /**
     * 执行章节生成 - 单步
     * 返回下一步应该执行什么
     */
    async executeNextChapter(bookId) {
        const state = this.workflows.get(bookId);
        if (!state)
            return { done: false, error: '工作流不存在' };
        if (state.pendingChapters.length === 0) {
            return { done: true };
        }
        // 取下一个待生成的章节
        const nextChapterNum = state.pendingChapters[0];
        this.updateState(bookId, {
            currentNode: 'writing_chapter',
            currentChapter: nextChapterNum,
        });
        try {
            const chapter = await book_generator_service_1.bookGeneratorService.generateChapter(bookId, nextChapterNum);
            // 更新工作流状态
            const newPending = state.pendingChapters.filter(n => n !== nextChapterNum);
            this.updateState(bookId, {
                currentNode: 'idle',
                completedChapters: state.completedChapters + 1,
                pendingChapters: newPending,
            });
            if (newPending.length === 0) {
                // 全部章节完成
                this.updateState(bookId, { phase: 'supplement' });
                return { done: true, chapter };
            }
            return { done: false, chapter, nextChapter: newPending[0] };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : '生成失败';
            // 标记本章失败，继续下一章
            const newPending = state.pendingChapters.filter(n => n !== nextChapterNum);
            const newFailed = [...state.failedChapters, nextChapterNum];
            this.updateState(bookId, {
                currentNode: 'idle',
                pendingChapters: newPending,
                failedChapters: newFailed,
            });
            if (newPending.length === 0) {
                return { done: true, error: `第${nextChapterNum}章失败` };
            }
            return { done: false, error: `第${nextChapterNum}章失败: ${errorMessage}` };
        }
    }
    /**
     * 执行全部章节生成（自动循环）
     */
    async executeAllChapters(bookId, onProgress) {
        const state = this.workflows.get(bookId);
        if (!state)
            return { success: false, completedCount: 0, failedCount: 0, errors: ['工作流不存在'] };
        const errors = [];
        while (true) {
            const currentState = this.workflows.get(bookId);
            if (!currentState || currentState.pendingChapters.length === 0) {
                break;
            }
            const nextChapter = currentState.pendingChapters[0];
            if (onProgress) {
                const progress = Math.round((currentState.completedChapters / currentState.totalChapters) * 100);
                onProgress(progress, nextChapter);
            }
            try {
                await book_generator_service_1.bookGeneratorService.generateChapter(bookId, nextChapter);
                const updated = this.workflows.get(bookId);
                if (updated) {
                    this.updateState(bookId, {
                        completedChapters: updated.completedChapters + 1,
                        pendingChapters: updated.pendingChapters.filter(n => n !== nextChapter),
                    });
                }
            }
            catch (error) {
                const errorMessage = error instanceof Error ? error.message : '生成失败';
                errors.push(`第${nextChapter}章: ${errorMessage}`);
                const updated = this.workflows.get(bookId);
                if (updated) {
                    this.updateState(bookId, {
                        pendingChapters: updated.pendingChapters.filter(n => n !== nextChapter),
                        failedChapters: [...updated.failedChapters, nextChapter],
                    });
                }
            }
        }
        this.updateState(bookId, { phase: 'supplement' });
        const finalState = this.workflows.get(bookId);
        return {
            success: errors.length === 0,
            completedCount: finalState?.completedChapters || 0,
            failedCount: finalState?.failedChapters?.length || 0,
            errors,
        };
    }
    /**
     * 生成前言
     */
    async executeForeword(bookId) {
        this.updateState(bookId, { currentNode: 'writing_foreword' });
        try {
            const foreword = await book_generator_service_1.bookGeneratorService.generateForeword(bookId);
            this.updateState(bookId, { currentNode: 'idle' });
            return { success: true, foreword };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : '生成前言失败';
            return { success: false, error: errorMessage };
        }
    }
    /**
     * 生成后记
     */
    async executeAfterword(bookId) {
        this.updateState(bookId, { currentNode: 'writing_afterword' });
        try {
            const afterword = await book_generator_service_1.bookGeneratorService.generateAfterword(bookId);
            this.updateState(bookId, { currentNode: 'idle' });
            return { success: true, afterword };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : '生成后记失败';
            return { success: false, error: errorMessage };
        }
    }
    /**
     * 完成工作流
     */
    finishWorkflow(bookId) {
        const state = this.workflows.get(bookId);
        if (!state)
            return undefined;
        this.updateState(bookId, { currentNode: 'finished', phase: 'done' });
        return this.workflows.get(bookId);
    }
    /**
     * 获取工作流进度
     */
    getProgress(bookId) {
        const state = this.workflows.get(bookId);
        if (!state)
            return null;
        return {
            phase: state.phase,
            currentNode: state.currentNode,
            currentChapter: state.currentChapter,
            completedChapters: state.completedChapters,
            totalChapters: state.totalChapters,
            pendingChapters: state.pendingChapters,
            failedChapters: state.failedChapters,
            progress: Math.round((state.completedChapters / state.totalChapters) * 100),
        };
    }
    /**
     * 取消工作流
     */
    cancelWorkflow(bookId) {
        return this.workflows.delete(bookId);
    }
}
// 导出单例
exports.workflowEngine = new BookWorkflowEngine();
