import type { BreakdownState } from '../breakdown/types' import type { Checkpoint } from './types' /** 按工作流隔离并排序,后端数组顺序变化不会影响恢复判断。 */ export function workflowCheckpoints(checkpoints: Checkpoint[], name: string): Checkpoint[] { return checkpoints .filter(item => item.workflowName === name) .toSorted((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt)) } /** 失败快照可能只有错误;回看最近有效状态,同时只使用最新记录的执行状态。 */ export function breakdownSnapshot(checkpoints: Checkpoint[]): BreakdownState | null { const records = workflowCheckpoints(checkpoints, 'breakdown') const latest = records.at(-1) if (!latest) return null const data = records .slice(-30) .toReversed() .find(item => item.state.runConfig || item.state.breakdownResult || item.state.storyboardPlans?.length) ?.state ?? {} return { ...data, workflowExecution: latest.state.workflowExecution } } /** 根据最近后端可恢复窗口推导操作;页面仍需先确认任务已经结束。 */ export function recoveryOptions(checkpoints: Checkpoint[]) { const records = workflowCheckpoints(checkpoints, 'breakdown').toReversed() const retry = records.slice(0, 10).some(item => item.state.tasks?.some(task => task.status === 'failed')) const shots = records .slice(0, 30) .some(({ state }) => Boolean( state.storyboardPlans?.length && state.subjectCandidates?.length && state.subjectForms && (state.storyboardEpisodeShots?.length ?? 0) < state.storyboardPlans.length ) ) const storyboard = records .slice(0, 10) .some(({ state }) => Boolean(state.storyboardEpisodeShots?.length && state.subjectCandidates?.length && state.subjectForms) ) return { retry, shots, storyboard } } /** 项目公开详情不含内部任务;按每条工作流的最新 checkpoint 检查活动执行。 */ export function hasRunningWorkflow(checkpoints: Checkpoint[]): boolean { return [...new Set(checkpoints.map(item => item.workflowName))].some( name => workflowCheckpoints(checkpoints, name).at(-1)?.state.workflowExecution?.status === 'running' ) }