289 lines
14 KiB
TypeScript
289 lines
14 KiB
TypeScript
import { useShotSelection } from './useShotSelection'
|
|
import { useWorkflowExecution } from '../workflows/execution'
|
|
import type { StoryboardBatchResult, PromptBatchResult, StoryboardShotRegenerationResult } from './types'
|
|
import { computed, ref, watch } from 'vue'
|
|
import { useQuery } from '../../composables/useQuery'
|
|
import { useProjectContext } from '../projects/context'
|
|
import { formalBreakdownSnapshot } from '../workflows/selectors'
|
|
import { getOperation, runOperation } from '../workflows/operations'
|
|
import { preparationApi } from '../storyboard-preparation/api'
|
|
import { currentStoryboardApproval } from '../storyboard-preparation/model'
|
|
import { storyboardApi } from './api'
|
|
import {
|
|
currentEpisodeSource,
|
|
getStoryboardSession,
|
|
invalidateEpisodeSource,
|
|
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, selectedShot } = useShotSelection(() => episodeNo.value)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const concurrency = ref(2)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const maxRepairAttempts = ref(2)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const force = ref(false)
|
|
/** 正式镜头查询只补充正式快照正文,不混入最新候选。 */
|
|
const snapshot = computed(() => formalBreakdownSnapshot(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 approval = useQuery(id, (projectId, signal) =>
|
|
projectId ? preparationApi.approval(projectId, signal) : Promise.resolve(null)
|
|
)
|
|
/** 有效审核必须绑定正式内容;旧记录或未知状态不放行。 */
|
|
const approved = computed(
|
|
() =>
|
|
!approval.loading.value && !approval.error.value && currentStoryboardApproval(approval.data.value, id.value)
|
|
)
|
|
/** 正式修订或审核后随项目数据刷新核对凭据,避免沿用旧权限。 */
|
|
watch(context.checkpoints, () => {
|
|
if (approval.data.value || approval.error.value) void approval.refresh()
|
|
})
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const session = computed(() => getStoryboardSession(id.value))
|
|
/** 单集重生成返回的是最新镜头正文,优先于不会随局部覆盖更新的历史 Breakdown 快照。 */
|
|
const source = computed(() =>
|
|
currentEpisodeSource(
|
|
id.value,
|
|
episodeNo.value,
|
|
sourceEpisodes.value.find(item => item.episodeNo === episodeNo.value)
|
|
)
|
|
)
|
|
/** 重生成接口未返回新正文时,明确标记辅助剧情信息不可用。 */
|
|
const episodeSourceUnavailable = computed(() => !!session.value.invalidatedEpisodeSources[episodeNo.value])
|
|
/** 当前剧集的正式镜头目录。 */
|
|
const shots = computed(() =>
|
|
mergeDesignedShots(data.value?.directions ?? null, data.value?.visualStates ?? null, source.value)
|
|
)
|
|
/** 明确指定的镜头不存在时不代选其它镜头;首次进入才选择第一镜。 */
|
|
const shot = computed(() =>
|
|
selectedShot.value ? 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 &&
|
|
data.value.directions.staleCount === 0
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
const visualStateComplete = computed(
|
|
() =>
|
|
!!data.value?.visualStates.shotCount &&
|
|
data.value.visualStates.visualStateCount === data.value.visualStates.shotCount &&
|
|
data.value.visualStates.coverage === 1 &&
|
|
data.value.visualStates.staleCount === 0
|
|
)
|
|
/** 当前工作区的响应式状态或派生业务条件。 */
|
|
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': approved.value && !blocked.value && prerequisites.value.directionProject && batchValid.value,
|
|
'visual-all':
|
|
approved.value &&
|
|
!blocked.value &&
|
|
prerequisites.value.visualProject &&
|
|
batchValid.value &&
|
|
repairsValid.value,
|
|
prompts: !blocked.value && shots.value.length > 0 && batchValid.value
|
|
}))
|
|
|
|
/** 正式后台任务观察器;重生成使用后端 breakdown 名称并通过 metadata 区分。 */
|
|
const executions = [
|
|
{ title: '单集镜头重生成', observer: useWorkflowExecution(id, 'breakdown') },
|
|
{ title: '项目导演设计', observer: useWorkflowExecution(id, 'storyboard-direction') },
|
|
{ title: '项目即时状态', observer: useWorkflowExecution(id, 'storyboard-visual-state') },
|
|
{ title: '项目视频提示词', observer: useWorkflowExecution(id, 'video-prompt') }
|
|
]
|
|
/** 真实终态结果才发布回执;刷新恢复也按后端 metadata 归属。 */
|
|
for (const [index, entry] of executions.entries()) {
|
|
watch(
|
|
() => entry.observer.query.data.value,
|
|
record => {
|
|
if (!record || record.projectId !== id.value || !['completed', 'failed'].includes(record.status)) return
|
|
const target = getStoryboardSession(record.projectId)
|
|
if (index === 0 && record.metadata?.action !== 'regenerate-storyboard-episode') return
|
|
if (record.status === 'completed' && record.result) {
|
|
if (index === 0) {
|
|
const result = record.result as unknown as StoryboardShotRegenerationResult
|
|
const number = record.metadata?.episodeNo
|
|
if (
|
|
typeof number !== 'number' ||
|
|
result.episodeNo !== number ||
|
|
!result.storyboardShotValidation ||
|
|
!result.storyboardShotSummary
|
|
)
|
|
return
|
|
target.receipt = { kind: 'shots', title: `第 ${number} 集镜头`, result }
|
|
if (result.storyboardShotValidation.valid) {
|
|
delete target.regeneratedEpisodes[number]
|
|
invalidateEpisodeSource(record.projectId, number)
|
|
target.prompts = {}
|
|
if (episodeNo.value === number) selectedShot.value = ''
|
|
}
|
|
} else if (index === 3) {
|
|
const result = record.result as unknown as PromptBatchResult
|
|
if (typeof result.failed !== 'number') return
|
|
target.receipt = { kind: 'prompts', title: entry.title, result }
|
|
} else {
|
|
const result = record.result as unknown as StoryboardBatchResult
|
|
if (result.projectId !== record.projectId || !Array.isArray(result.episodes)) return
|
|
target.receipt = { kind: 'batch', title: entry.title, result }
|
|
}
|
|
}
|
|
void Promise.all([query.refresh(), context.refresh(), context.refreshStageReadiness()])
|
|
}
|
|
)
|
|
}
|
|
|
|
/** 捕获提交时的目标与配置,切换剧集不改变正在运行的请求或回执。 */
|
|
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 expectedApproval = JSON.stringify(approval.data.value?.evidence)
|
|
const titles: Record<StoryboardCommand, string> = {
|
|
'shots-current': `第 ${targetEpisode} 集镜头`,
|
|
'direction-all': '项目导演设计',
|
|
'visual-all': '项目即时状态',
|
|
prompts: '项目视频提示词'
|
|
}
|
|
target.receipt = null
|
|
await runOperation(projectId, `生成${titles[command]}`, async () => {
|
|
if (command === 'direction-all' || command === 'visual-all') {
|
|
const latest = await preparationApi.approval(projectId)
|
|
if (
|
|
id.value !== projectId ||
|
|
!currentStoryboardApproval(latest, projectId) ||
|
|
JSON.stringify(latest.evidence) !== expectedApproval
|
|
)
|
|
throw new Error('正式分镜审核已变化,请重新进行制作前检查并核对费用。')
|
|
}
|
|
if (command === 'shots-current') {
|
|
const started = await storyboardApi.regenerateEpisodeShots(projectId, targetEpisode)
|
|
await executions[0]!.observer.track(started.executionId, projectId)
|
|
} else if (command === 'direction-all') {
|
|
const started = await storyboardApi.generateDirections(projectId, batch)
|
|
await executions[1]!.observer.track(started.executionId, projectId)
|
|
} else if (command === 'visual-all') {
|
|
const started = await storyboardApi.generateVisualStates(projectId, {
|
|
...batch,
|
|
maxRepairAttempts: repairs
|
|
})
|
|
await executions[2]!.observer.track(started.executionId, projectId)
|
|
} else {
|
|
target.prompts = {}
|
|
const started = await storyboardApi.generatePrompts(projectId, batch)
|
|
await executions[3]!.observer.track(started.executionId, projectId)
|
|
}
|
|
})
|
|
await Promise.all([query.refresh(), context.refreshStageReadiness()])
|
|
}
|
|
|
|
return {
|
|
id,
|
|
selectedEpisode,
|
|
episodeNo,
|
|
episodeOptions,
|
|
selectedShot,
|
|
shots,
|
|
shot,
|
|
query,
|
|
data,
|
|
source,
|
|
episodeSourceUnavailable,
|
|
prerequisites,
|
|
session,
|
|
operation,
|
|
concurrency,
|
|
maxRepairAttempts,
|
|
force,
|
|
allowed,
|
|
batchValid,
|
|
repairsValid,
|
|
blocked,
|
|
directionComplete,
|
|
approval,
|
|
approved,
|
|
visualStateComplete,
|
|
hasCurrentEpisodeShots,
|
|
executions,
|
|
run
|
|
}
|
|
}
|