import { reactive } from 'vue' import type { EpisodeShots } from '../breakdown/types' import type { Checkpoint } from '../workflows/types' import { workflowCheckpoints } from '../workflows/selectors' import type { DesignedShot, EpisodeDirections, EpisodeVisualStates, StoryboardSession } from './types' /** 仅保存本会话回执和已返回的 Prompt,切换路由不丢失长请求结果。 */ const sessions = reactive>({}) /** * 恢复整个项目已失效的剧集集合,兼容隐私模式或损坏的浏览器缓存。 * @param projectId 正式项目 ID * @returns 只包含正整数剧集编号的失效标记 */ function savedEpisodeInvalidations(projectId: string): Record { try { if (typeof localStorage === 'undefined') return {} const value: unknown = JSON.parse( localStorage.getItem(`short-drama:storyboard-invalidated:${projectId}`) ?? '[]' ) if (!Array.isArray(value)) return {} return Object.fromEntries( value .filter(number => typeof number === 'number' && Number.isSafeInteger(number) && number > 0) .map(number => [number, true]) ) } catch { return {} } } /** 获取指定项目的独立回执容器。 */ export function getStoryboardSession(id: string): StoryboardSession { return (sessions[id] ??= { receipt: null, prompts: {}, regeneratedEpisodes: {}, invalidatedEpisodeSources: savedEpisodeInvalidations(id) }) } /** * 根据真实有效终态废弃旧正文,并持久化全部剧集,隐藏任务不影响恢复。 * @param projectId 正式项目 ID * @param episodeNo 已有效重生成的剧集编号 */ export function invalidateEpisodeSource(projectId: string, episodeNo: number) { if (!Number.isSafeInteger(episodeNo) || episodeNo <= 0) return const session = getStoryboardSession(projectId) delete session.regeneratedEpisodes[episodeNo] session.invalidatedEpisodeSources = { ...savedEpisodeInvalidations(projectId), ...session.invalidatedEpisodeSources, [episodeNo]: true } try { if (typeof localStorage !== 'undefined') localStorage.setItem( `short-drama:storyboard-invalidated:${projectId}`, JSON.stringify(Object.keys(session.invalidatedEpisodeSources).map(Number)) ) } catch { /* 浏览器禁止存储时仍保留当前会话保护。 */ } } /** * 仅可信完整重新拆解完成后清除旧失效集合。 * @param projectId 正式项目 ID */ export function clearEpisodeSourceInvalidations(projectId: string) { getStoryboardSession(projectId).invalidatedEpisodeSources = {} try { if (typeof localStorage !== 'undefined') localStorage.removeItem(`short-drama:storyboard-invalidated:${projectId}`) } catch { /* 当前会话已经清除,存储限制不会触发业务请求。 */ } } /** * 重生成后不向新 Shot ID 回填旧 checkpoint 的标题、剧情或时长。 * @param projectId 当前正式项目 ID * @param episodeNo 当前剧集编号 * @param fallback 未失效的原始分集快照 * @returns 实际正文或缺省;缺省时仅展示正式镜头编号 */ export function currentEpisodeSource(projectId: string, episodeNo: number, fallback?: EpisodeShots) { const session = getStoryboardSession(projectId) return ( session.regeneratedEpisodes[episodeNo] ?? (session.invalidatedEpisodeSources[episodeNo] ? undefined : fallback) ) } /** 后端生成服务只读取最新 Breakdown;不能用历史可恢复快照冒充当前生成前置条件。 */ export function storyboardPrerequisites(checkpoints: Checkpoint[], episodeNo: number) { const latest = workflowCheckpoints(checkpoints, 'breakdown').at(-1) const state = latest?.state /** 候选只供阅读和修订,提交前不能充当正式制作输入。 */ const uncommitted = state?.candidateOnly === true && state.storyboardPersisted !== true && state.candidateCommitted !== true const directionEpisodes = state?.breakdownResult?.storyboardEpisodeShots ?? [] const visualEpisodes = state?.storyboardEpisodeShots ?? [] return { directionEpisode: !uncommitted && directionEpisodes.some(item => item.episodeNo === episodeNo), directionProject: !uncommitted && directionEpisodes.length > 0, visualEpisode: !uncommitted && visualEpisodes.some(item => item.episodeNo === episodeNo) && !!state?.subjectCandidates?.length && !!state?.subjectForms?.length, visualProject: !uncommitted && visualEpisodes.length > 0 && !!state?.subjectCandidates?.length && !!state?.subjectForms?.length, breakdownRunning: state?.workflowExecution?.status === 'running' } } /** 以正式 Shot ID 关联设计和状态;checkpoint 仅补足标题与剧情描述。 */ export function mergeDesignedShots( directions: EpisodeDirections | null, states: EpisodeVisualStates | null, source?: EpisodeShots ): DesignedShot[] { const rows = new Map() for (const beat of directions?.beats ?? []) { for (const shot of beat.shots) { const detail = source?.beatShots .find(item => item.beatNo === beat.beatNo) ?.shots.find(item => item.shotNo === shot.shotNo) rows.set(shot.shotId, { shotId: shot.shotId, shotNo: shot.shotNo, beatNo: beat.beatNo, title: detail?.title || `镜头 ${shot.shotNo}`, description: detail?.description || '', visualFocus: detail?.visualFocus || '', durationSeconds: detail?.durationSeconds, direction: shot.direction, visualState: null }) } } for (const beat of states?.beats ?? []) { for (const shot of beat.shots) { const row = rows.get(shot.shotId) if (row) row.visualState = shot.visualState else rows.set(shot.shotId, { shotId: shot.shotId, shotNo: shot.shotNo, beatNo: beat.beatNo, title: `镜头 ${shot.shotNo}`, description: '', visualFocus: '', direction: null, visualState: shot.visualState }) } } return [...rows.values()].toSorted((a, b) => a.beatNo - b.beatNo || a.shotNo - b.shotNo) } /** 摄影枚举翻译;新枚举保留原值,避免静默丢失字段。 */ export function directionLabel(value: string, field?: string): string { if (value === 'wide') return field === 'lensIntent' ? '广角' : '远景' const labels: Record = { 'extreme-wide': '大远景', full: '全景', medium: '中景', 'medium-close-up': '近景', 'close-up': '特写', 'extreme-close-up': '大特写', 'eye-level': '平视', 'high-angle': '俯拍', 'low-angle': '仰拍', overhead: '顶拍', 'dutch-angle': '倾斜机位', static: '固定', pan: '水平摇镜', tilt: '垂直摇镜', 'push-in': '推进', 'pull-out': '拉远', tracking: '跟拍', dolly: '移动摄影', handheld: '手持', crane: '升降', 'ultra-wide': '超广角', normal: '标准', portrait: '人像焦段', telephoto: '长焦', deep: '深景深', moderate: '中等景深', shallow: '浅景深' } return labels[value] ?? value } /** 保留已有导出路径;图库与分镜共用同一套图片地址校验。 */ export { referenceImageUrl } from '../../lib/assets'