import type { ProjectDetail } from '../projects/types' import type { WorkflowExecution, WorkflowExecutionStatus } from '../workflows/execution.types' import type { Checkpoint } from '../workflows/types' /** 创作阶段的只读展示,状态沿用服务端执行枚举,未知时保留空值。 */ export interface DramaStage { /** 正式工作流节点。 */ node: string /** 中文阶段名称。 */ label: string /** 来自执行记录或已保存输出的状态。 */ status: WorkflowExecutionStatus | null /** 面向用户的阶段说明。 */ statusLabel: string /** 当前阶段是否明确审核通过或保存完成。 */ done: boolean /** 节点失败或审核未通过,需要使用错误样式。 */ error: boolean } /** 创作流程展示顺序只用于排版,不能按位置补齐前序节点的成功状态。 */ const nodes = [ ['generateStoryPlan', '故事规划'], ['generateEpisode', '编写剧集'], ['generateCharacter', '角色设定'], ['generateWorld', '世界观'], ['reviewDrama', '审核与改写'], ['finishProject', '完成'] ] as const /** 改写相关节点归入已有审核阶段,不影响其它节点的状态。 */ const reviewNodes = ['reviewDrama', 'generateRewriteSuggestion', 'rewrite', 'resumeRewrite'] /** * 合并当前执行与最新保存记录,既不由节点顺序推断成功,也不复用新任务之前的旧审核结果。 * @param project 正式项目详情 * @param checkpoint 当前创作工作流的最新保存记录 * @param execution 当前持久化任务 * @returns 各阶段的只读展示 */ export function dramaStages( project: ProjectDetail | null, checkpoint: Checkpoint | undefined, execution: WorkflowExecution | null ): DramaStage[] { const state = checkpoint?.state const currentSnapshot = !execution || (!!checkpoint && Date.parse(checkpoint.createdAt) >= Date.parse(execution.startedAt ?? execution.createdAt)) const saved = currentSnapshot ? state : undefined const count = project?.episodeCount ?? saved?.episodeCount const completed: Record = { // 恢复服务明确复用最新保存记录中的规划;不从 Episode、Character 或执行顺序反推规划成功。 generateStoryPlan: !!state?.storyPlan && (currentSnapshot || execution?.metadata?.action === 'resume-generation' || execution?.metadata?.action === 'resume-rewrite'), generateEpisode: !!count && (saved?.episodes?.length ?? 0) >= count, generateCharacter: !!saved?.characters, generateWorld: !!saved?.world, reviewDrama: typeof saved?.reviewPassed === 'boolean', finishProject: project?.status === 'completed' } return nodes.map(([node, label]) => { const current = execution?.stage === node || (node === 'reviewDrama' && reviewNodes.includes(execution?.stage ?? '')) const status: WorkflowExecutionStatus | null = current && execution?.status !== 'completed' ? (execution?.status ?? null) : completed[node] ? 'completed' : execution && ['pending', 'running'].includes(execution.status) ? 'pending' : null const rejected = node === 'reviewDrama' && status === 'completed' && saved?.reviewPassed === false return { node, label, status, statusLabel: rejected ? '未通过' : status === null ? '待确认' : { pending: '等待更新', running: '进行中', completed: '已完成', failed: '失败' }[status], done: status === 'completed' && !rejected, error: status === 'failed' || rejected } }) }