315 lines
15 KiB
TypeScript
315 lines
15 KiB
TypeScript
import { useShotSelection } from '../storyboard/useShotSelection'
|
|
import type { KeyframeBatchResult } from './types'
|
|
import type { PromptBatchResult } from '../storyboard/types'
|
|
import { computed, ref, watch } from 'vue'
|
|
import { usePolling } from '../../composables/usePolling'
|
|
import { useProjectContext } from '../projects/context'
|
|
import { currentEpisodeSource, getStoryboardSession, mergeDesignedShots } from '../storyboard/model'
|
|
import { storyboardApi } from '../storyboard/api'
|
|
import { breakdownSnapshot } from '../workflows/selectors'
|
|
import { getOperation, runOperation } from '../workflows/operations'
|
|
import { useWorkflowExecution } from '../workflows/execution'
|
|
import { productionApi } from './api'
|
|
import { getProductionSession, validOptionalSize } from './model'
|
|
import type { VideoInputMode } from './types'
|
|
import { errorMessage } from '../../lib/http'
|
|
|
|
/** 项目级生产动作;每次请求都由统一项目锁防止重复提交。 */
|
|
export type ProductionCommand = 'prompts' | 'keyframes' | 'videos' | 'retry-videos'
|
|
|
|
/** 镜头生产页的查询、筛选、就绪状态与批量操作。 */
|
|
export function useProduction() {
|
|
const context = useProjectContext()
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const id = computed(() => context.project.value?.id ?? '')
|
|
/** 两个镜头工作区共用地址选择,刷新恢复同一正式镜头。 */
|
|
const { selectedEpisode, selectedShot } = useShotSelection(() => episodeNo.value)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const concurrency = ref(2)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const force = ref(false)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const keyframeLimit = ref<number | ''>('')
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const keyframeWidth = ref<number | ''>('')
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const keyframeHeight = ref<number | ''>('')
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const promptLimit = ref<number | null>(1)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const videoLimit = ref<number | null>(1)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const videoRepairAttempts = ref<number | null>(2)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const videoInputMode = ref<VideoInputMode | 'all'>('all')
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const snapshot = computed(() => breakdownSnapshot(context.checkpoints.value))
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const sourceEpisodes = computed(
|
|
() => snapshot.value?.storyboardEpisodeShots ?? snapshot.value?.breakdownResult?.storyboardEpisodeShots ?? []
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const episodeOptions = computed(() => {
|
|
const entries = new Map(context.project.value?.episodes.map(episode => [episode.episode, episode.title]) ?? [])
|
|
for (const episode of sourceEpisodes.value) {
|
|
if (!entries.has(episode.episodeNo)) entries.set(episode.episodeNo, episode.episodePlan.episodeTitle)
|
|
}
|
|
return [...entries]
|
|
.map(([episodeNo, title]) => ({ episodeNo, title }))
|
|
.toSorted((a, b) => a.episodeNo - b.episodeNo)
|
|
})
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const episodeNo = computed(
|
|
() =>
|
|
episodeOptions.value.find(item => item.episodeNo === selectedEpisode.value)?.episodeNo ??
|
|
episodeOptions.value[0]?.episodeNo ??
|
|
0
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const queryKey = computed(() => JSON.stringify([id.value, episodeNo.value, force.value]))
|
|
/** 就绪状态在进入、切集或自身操作完成后读取,不定时刷新。 */
|
|
const query = usePolling(
|
|
queryKey,
|
|
async (key, signal) => {
|
|
const [projectId, number, overwrite] = JSON.parse(key) as [string, number, boolean]
|
|
if (!projectId || !number) return null
|
|
const [directions, prompts, keyframes, videos, videoStatus, videoInputPlanState] = await Promise.all([
|
|
storyboardApi.directions(projectId, number, signal),
|
|
productionApi.promptReadiness(projectId, overwrite, signal),
|
|
productionApi.keyframeReadiness(projectId, overwrite, signal),
|
|
productionApi.videoReadiness(projectId, overwrite, signal),
|
|
productionApi.projectVideoStatus(projectId, signal),
|
|
productionApi
|
|
.videoInputPlans(projectId, number, signal)
|
|
.then(data => ({ data, error: '' }))
|
|
.catch(cause => ({ data: null, error: errorMessage(cause) }))
|
|
])
|
|
if (directions.projectId !== projectId || directions.episodeNo !== number)
|
|
throw new Error('镜头生产查询返回了不匹配的项目或剧集,请刷新后重试。')
|
|
return {
|
|
directions,
|
|
prompts,
|
|
keyframes,
|
|
videos,
|
|
videoStatus,
|
|
videoInputPlans: videoInputPlanState.data,
|
|
videoInputPlansError: videoInputPlanState.error
|
|
}
|
|
},
|
|
false
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
/** 重生成后不拿旧剧情快照补充新的正式镜头。 */
|
|
const source = computed(() =>
|
|
currentEpisodeSource(
|
|
id.value,
|
|
episodeNo.value,
|
|
sourceEpisodes.value.find(item => item.episodeNo === episodeNo.value)
|
|
)
|
|
)
|
|
/** 未取得重生成后的正文时,只展示正式镜头编号,不填充旧剧情或时长。 */
|
|
const episodeSourceUnavailable = computed(
|
|
() => !!getStoryboardSession(id.value).invalidatedEpisodeSources[episodeNo.value]
|
|
)
|
|
/** 当前剧集的正式镜头目录。 */
|
|
const shots = computed(() => mergeDesignedShots(query.data.value?.directions ?? null, null, source.value))
|
|
/** 明确指定的镜头不存在时不代选其它镜头;首次进入才选择第一镜。 */
|
|
const shot = computed(() =>
|
|
selectedShot.value ? shots.value.find(item => item.shotId === selectedShot.value) : shots.value[0]
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const promptItem = computed(() => query.data.value?.prompts.items.find(item => item.shotId === shot.value?.shotId))
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const keyframeItem = computed(() =>
|
|
query.data.value?.keyframes.items.find(item => item.shotId === shot.value?.shotId)
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const videoItem = computed(() => query.data.value?.videos.items.find(item => item.shotId === shot.value?.shotId))
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const videoStatusItem = computed(() =>
|
|
query.data.value?.videoStatus.items.find(item => item.shotId === shot.value?.shotId)
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const operation = computed(() => getOperation(id.value))
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const session = computed(() => getProductionSession(id.value))
|
|
/** 普通首帧后台观察器,刷新页面后恢复真实执行状态。 */
|
|
const keyframeExecution = useWorkflowExecution(id, 'keyframe-generation')
|
|
const promptExecution = useWorkflowExecution(id, 'video-prompt')
|
|
const videoExecution = useWorkflowExecution(id, 'video-quality')
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const batchValid = computed(() => Number.isSafeInteger(concurrency.value) && concurrency.value > 0)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const promptValid = computed(() => Number.isSafeInteger(promptLimit.value) && (promptLimit.value ?? 0) > 0)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const keyframeValid = computed(
|
|
() =>
|
|
(keyframeLimit.value === '' || (Number.isSafeInteger(keyframeLimit.value) && keyframeLimit.value > 0)) &&
|
|
validOptionalSize(keyframeWidth.value, keyframeHeight.value)
|
|
)
|
|
/** 视频质量流水线要求有限的提交范围和至少一次自动修复额度。 */
|
|
const videoQualityValid = computed(
|
|
() =>
|
|
Number.isSafeInteger(videoLimit.value) &&
|
|
(videoLimit.value ?? 0) > 0 &&
|
|
Number.isSafeInteger(videoRepairAttempts.value) &&
|
|
(videoRepairAttempts.value ?? 0) > 0
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const blocked = computed(
|
|
() =>
|
|
operation.value.pending ||
|
|
promptExecution.active.value ||
|
|
videoExecution.active.value ||
|
|
!!context.error.value ||
|
|
!!query.error.value ||
|
|
!query.data.value ||
|
|
context.project.value?.status !== 'completed'
|
|
)
|
|
|
|
/** 后台质量任务收口后重新读取资产;失败也要保留已经成功的镜头状态。 */
|
|
watch(
|
|
() => videoExecution.query.data.value?.status,
|
|
(status, previous) => {
|
|
if ((status === 'completed' || status === 'failed') && status !== previous) void query.refresh()
|
|
}
|
|
)
|
|
/** 观察后台状态变化并同步正式资产。 */
|
|
watch(
|
|
() => promptExecution.query.data.value?.status,
|
|
(status, previous) => {
|
|
const record = promptExecution.query.data.value
|
|
if (status === 'completed' && record?.projectId === id.value && record.result) {
|
|
const result = record.result as unknown as PromptBatchResult
|
|
if (typeof result.total === 'number' && Array.isArray(result.failures)) {
|
|
getProductionSession(record.projectId).receipt = {
|
|
kind: 'prompts',
|
|
title: '项目视频提示词',
|
|
result
|
|
}
|
|
}
|
|
}
|
|
if ((status === 'completed' || status === 'failed') && status !== previous) void query.refresh()
|
|
}
|
|
)
|
|
|
|
/** 普通首帧任务完成后才展示后端回执;失败仍刷新已经保存的资产。 */
|
|
watch(
|
|
() => keyframeExecution.query.data.value,
|
|
record => {
|
|
if (!record || record.projectId !== id.value || !['completed', 'failed'].includes(record.status)) return
|
|
if (record.status === 'completed' && record.result) {
|
|
const result = record.result as unknown as KeyframeBatchResult
|
|
if (typeof result.total !== 'number' || !Array.isArray(result.failures)) return
|
|
getProductionSession(record.projectId).receipt = { kind: 'keyframes', title: '项目首帧生成', result }
|
|
}
|
|
void query.refresh()
|
|
}
|
|
)
|
|
|
|
/** 批量操作只向就绪镜头提交;最终资产状态由轮询查询确认。 */
|
|
async function run(command: ProductionCommand) {
|
|
if (
|
|
blocked.value ||
|
|
!batchValid.value ||
|
|
(command === 'prompts' && !promptValid.value) ||
|
|
(command === 'keyframes' && !keyframeValid.value) ||
|
|
(command === 'videos' && !videoQualityValid.value)
|
|
)
|
|
return
|
|
const projectId = id.value
|
|
const input = { concurrency: concurrency.value, force: force.value }
|
|
const target = getProductionSession(projectId)
|
|
target.receipt = null
|
|
const staleKeyframes = query.data.value?.keyframes.stalePrimaryKeyframe ?? 0
|
|
const staleVideos = query.data.value?.videos.stalePrimaryVideo ?? 0
|
|
const labels: Record<ProductionCommand, string> = {
|
|
prompts: force.value ? '重生成项目视频提示词' : '补齐项目视频提示词',
|
|
keyframes: force.value
|
|
? '新增项目首帧候选'
|
|
: staleKeyframes > 0
|
|
? `更新 ${staleKeyframes} 个过期主首帧`
|
|
: `补齐第 ${episodeNo.value} 集主首帧`,
|
|
videos: force.value
|
|
? '新增项目质量视频候选'
|
|
: staleVideos > 0
|
|
? `更新 ${staleVideos} 个过期主视频`
|
|
: '提交视频质量任务',
|
|
'retry-videos': '重试失败视频任务'
|
|
}
|
|
await runOperation(projectId, labels[command], async () => {
|
|
if (command === 'prompts') {
|
|
const started = await productionApi.startPromptExecution(projectId, {
|
|
...input,
|
|
episodeNo: episodeNo.value,
|
|
limit: promptLimit.value ?? undefined
|
|
})
|
|
await promptExecution.track(started.executionId, projectId)
|
|
} else if (command === 'keyframes') {
|
|
const started = await productionApi.generateKeyframes(projectId, {
|
|
...input,
|
|
// 有 stale 时由后端优先处理全部 stale;否则只补当前剧集,避免误跑整个项目。
|
|
...(force.value || staleKeyframes > 0 ? {} : { episodeNo: episodeNo.value }),
|
|
...(keyframeLimit.value === '' ? {} : { limit: keyframeLimit.value }),
|
|
...(keyframeWidth.value === '' || keyframeHeight.value === ''
|
|
? {}
|
|
: { width: keyframeWidth.value, height: keyframeHeight.value })
|
|
})
|
|
await keyframeExecution.track(started.executionId, projectId)
|
|
} else if (command === 'videos') {
|
|
const started = await productionApi.startQualityVideoExecution(projectId, {
|
|
...input,
|
|
episodeNo: episodeNo.value,
|
|
limit: videoLimit.value ?? undefined,
|
|
...(videoInputMode.value === 'all' ? {} : { inputMode: videoInputMode.value }),
|
|
maxRepairAttempts: videoRepairAttempts.value ?? 0
|
|
})
|
|
await videoExecution.track(started.executionId, projectId)
|
|
} else {
|
|
target.receipt = {
|
|
kind: 'retry',
|
|
title: labels[command],
|
|
result: await productionApi.retryVideos(projectId, concurrency.value)
|
|
}
|
|
}
|
|
})
|
|
await query.refresh()
|
|
}
|
|
|
|
return {
|
|
id,
|
|
selectedEpisode,
|
|
selectedShot,
|
|
episodeNo,
|
|
episodeOptions,
|
|
shots,
|
|
shot,
|
|
promptItem,
|
|
keyframeItem,
|
|
videoItem,
|
|
videoStatusItem,
|
|
query,
|
|
episodeSourceUnavailable,
|
|
operation,
|
|
session,
|
|
concurrency,
|
|
force,
|
|
keyframeLimit,
|
|
keyframeWidth,
|
|
keyframeHeight,
|
|
promptLimit,
|
|
videoLimit,
|
|
videoRepairAttempts,
|
|
videoInputMode,
|
|
keyframeValid,
|
|
promptValid,
|
|
videoQualityValid,
|
|
batchValid,
|
|
blocked,
|
|
run,
|
|
keyframeExecution,
|
|
promptExecution,
|
|
videoExecution
|
|
}
|
|
}
|