196 lines
8.8 KiB
TypeScript
196 lines
8.8 KiB
TypeScript
import { computed, ref } from 'vue'
|
|
import { useQuery } from '../../composables/useQuery'
|
|
import { useProjectContext } from '../projects/context'
|
|
import { breakdownSnapshot } from '../workflows/selectors'
|
|
import { getOperation, runOperation } from '../workflows/operations'
|
|
import { storyboardApi } from './api'
|
|
import { getStoryboardSession, mergeDesignedShots, storyboardPrerequisites } from './model'
|
|
|
|
/** 分镜页只提供正式主流程命令,不暴露后端测试路由。 */
|
|
export type StoryboardCommand = 'shots-current' | 'direction-all' | 'visual-all' | 'prompts'
|
|
|
|
/** 分镜查询、依赖判断与操作入口;不修改后端 graph。 */
|
|
export function useStoryboard() {
|
|
const context = useProjectContext()
|
|
const id = computed(() => context.project.value?.id ?? '')
|
|
const selectedEpisode = ref<number>()
|
|
const selectedShot = ref('')
|
|
const concurrency = ref(2)
|
|
const maxRepairAttempts = ref(2)
|
|
const force = ref(false)
|
|
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]))
|
|
/** 分镜页仅在进入、切换剧集或显式刷新时读取。 */
|
|
const query = useQuery(queryKey, async (key, signal) => {
|
|
const [projectId, number] = JSON.parse(key) as [string, number]
|
|
if (!projectId || !number) return null
|
|
const [directions, visualStates] = await Promise.all([
|
|
storyboardApi.directions(projectId, number, signal),
|
|
storyboardApi.visualStates(projectId, number, signal)
|
|
])
|
|
if (
|
|
directions.projectId !== projectId ||
|
|
visualStates.projectId !== projectId ||
|
|
directions.episodeNo !== number ||
|
|
visualStates.episodeNo !== number
|
|
) {
|
|
throw new Error('分镜查询返回了不匹配的项目或剧集,请刷新后重试。')
|
|
}
|
|
return { directions, visualStates }
|
|
})
|
|
const data = query.data
|
|
const session = computed(() => getStoryboardSession(id.value))
|
|
/** 单集重生成返回的是最新镜头正文,优先于不会随局部覆盖更新的历史 Breakdown 快照。 */
|
|
const source = computed(
|
|
() =>
|
|
session.value.regeneratedEpisodes[episodeNo.value] ??
|
|
sourceEpisodes.value.find(item => item.episodeNo === episodeNo.value)
|
|
)
|
|
const shots = computed(() =>
|
|
mergeDesignedShots(data.value?.directions ?? null, data.value?.visualStates ?? null, source.value)
|
|
)
|
|
const shot = computed(() => shots.value.find(item => item.shotId === selectedShot.value) ?? shots.value[0])
|
|
const prerequisites = computed(() => storyboardPrerequisites(context.checkpoints.value, episodeNo.value))
|
|
const operation = computed(() => getOperation(id.value))
|
|
const batchValid = computed(() => Number.isSafeInteger(concurrency.value) && concurrency.value > 0)
|
|
const repairsValid = computed(() => Number.isSafeInteger(maxRepairAttempts.value) && maxRepairAttempts.value >= 0)
|
|
const blocked = computed(
|
|
() =>
|
|
operation.value.pending ||
|
|
!!context.error.value ||
|
|
!!query.error.value ||
|
|
!data.value ||
|
|
context.project.value?.status !== 'completed' ||
|
|
prerequisites.value.breakdownRunning
|
|
)
|
|
const directionComplete = computed(
|
|
() =>
|
|
!!data.value?.directions.shotCount &&
|
|
data.value.directions.directionCount === data.value.directions.shotCount &&
|
|
data.value.directions.coverage === 1
|
|
)
|
|
const visualStateComplete = computed(
|
|
() =>
|
|
!!data.value?.visualStates.shotCount &&
|
|
data.value.visualStates.visualStateCount === data.value.visualStates.shotCount &&
|
|
data.value.visualStates.coverage === 1
|
|
)
|
|
const hasCurrentEpisodeShots = computed(
|
|
() => Math.max(data.value?.directions.shotCount ?? 0, data.value?.visualStates.shotCount ?? 0) > 0
|
|
)
|
|
const allowed = computed<Record<StoryboardCommand, boolean>>(() => ({
|
|
'shots-current': !blocked.value && episodeNo.value > 0 && hasCurrentEpisodeShots.value,
|
|
'direction-all': !blocked.value && prerequisites.value.directionProject && batchValid.value,
|
|
'visual-all': !blocked.value && prerequisites.value.visualProject && batchValid.value && repairsValid.value,
|
|
prompts: !blocked.value && shots.value.length > 0 && batchValid.value
|
|
}))
|
|
|
|
/** 捕获提交时的目标与配置,切换剧集不改变正在运行的请求或回执。 */
|
|
async function run(command: StoryboardCommand) {
|
|
if (!allowed.value[command]) return
|
|
const projectId = id.value
|
|
const batch = { concurrency: concurrency.value, force: force.value }
|
|
const repairs = maxRepairAttempts.value
|
|
const targetEpisode = episodeNo.value
|
|
const target = getStoryboardSession(projectId)
|
|
const titles: Record<StoryboardCommand, string> = {
|
|
'shots-current': `第 ${targetEpisode} 集镜头`,
|
|
'direction-all': '项目导演设计',
|
|
'visual-all': '项目即时状态',
|
|
prompts: '项目视频提示词'
|
|
}
|
|
target.receipt = null
|
|
await runOperation(projectId, `生成${titles[command]}`, async () => {
|
|
switch (command) {
|
|
case 'shots-current': {
|
|
const result = await storyboardApi.regenerateEpisodeShots(projectId, targetEpisode)
|
|
if (result.projectId !== projectId || result.episodeNo !== targetEpisode) {
|
|
throw new Error('镜头重生成返回了不匹配的项目或剧集,请刷新后重试。')
|
|
}
|
|
target.receipt = { kind: 'shots', title: titles[command], result }
|
|
if (!result.storyboardShotValidation.valid) {
|
|
throw new Error('本集镜头校验未通过,后端未覆盖正式 Storyboard。')
|
|
}
|
|
target.regeneratedEpisodes[targetEpisode] = result.episodeShot
|
|
// 新镜头会获得新的数据库 ID,废弃所有按旧 Shot ID 缓存的提示词。
|
|
target.prompts = {}
|
|
if (episodeNo.value === targetEpisode) selectedShot.value = ''
|
|
break
|
|
}
|
|
case 'direction-all':
|
|
target.receipt = {
|
|
kind: 'batch',
|
|
title: titles[command],
|
|
result: await storyboardApi.generateDirections(projectId, batch)
|
|
}
|
|
break
|
|
case 'visual-all':
|
|
target.receipt = {
|
|
kind: 'batch',
|
|
title: titles[command],
|
|
result: await storyboardApi.generateVisualStates(projectId, {
|
|
...batch,
|
|
maxRepairAttempts: repairs
|
|
})
|
|
}
|
|
break
|
|
case 'prompts':
|
|
// 断网时也可能已部分覆盖,先清空缓存,避免继续展示可能失效的正文。
|
|
target.prompts = {}
|
|
target.receipt = {
|
|
kind: 'prompts',
|
|
title: titles[command],
|
|
result: await storyboardApi.generatePrompts(projectId, batch)
|
|
}
|
|
break
|
|
}
|
|
})
|
|
await Promise.all([query.refresh(), context.refreshStageReadiness()])
|
|
}
|
|
|
|
return {
|
|
id,
|
|
selectedEpisode,
|
|
episodeNo,
|
|
episodeOptions,
|
|
selectedShot,
|
|
shots,
|
|
shot,
|
|
query,
|
|
data,
|
|
source,
|
|
prerequisites,
|
|
session,
|
|
operation,
|
|
concurrency,
|
|
maxRepairAttempts,
|
|
force,
|
|
allowed,
|
|
batchValid,
|
|
repairsValid,
|
|
blocked,
|
|
directionComplete,
|
|
visualStateComplete,
|
|
hasCurrentEpisodeShots,
|
|
run
|
|
}
|
|
}
|