Files
short-drama-agent-front/src/features/breakdown/components/BreakdownCandidatePanel.vue
T

153 lines
9.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, onScopeDispose, ref, watch } from 'vue'
import { NButton, NCheckbox } from 'naive-ui'
import { AppDialog, WorkspaceRefreshButton } from '../../../components/ui'
import type { BreakdownState } from '../types'
import type { BreakdownQuality } from '../quality'
import type { CandidateCommitPreview } from '../candidate.types'
import { candidateCheckKeys, candidateCheckLabels, candidatePreviewReady } from '../candidate'
import { breakdownApi } from '../api'
import { runOperation, getOperation } from '../../workflows/operations'
import { projectsApi } from '../../projects/api'
import { workflowCheckpoints } from '../../workflows/selectors'
import { errorMessage } from '../../../lib/http'
import { showActionFeedback, useActionMessage } from '../../../components/ui/useActionMessage'
/** 当前原始版本与集中审核结论共同决定安全检查入口。 */
const props = defineProps<{
/** 当前项目。 */
projectId: string
/** 当前版本归一化候选。 */
state?: BreakdownState
/** 当前检查点。 */
checkpointId?: string
/** 页面统一质量结论。 */
quality: BreakdownQuality
/** 活动任务或读取失败时禁止写入。 */
disabled: boolean
}>()
/** 只在真实写回后刷新服务端资产及阶段状态。 */
const emit = defineEmits<{ refresh: [] }>()
/** 应用消息层。 */
const message = useActionMessage()
/** 项目级写操作锁。 */
const operation = computed(() => getOperation(props.projectId))
/** 当前预检查回执。 */
const preview = ref<CandidateCommitPreview | null>(null)
/** 只读安全检查加载状态。 */
const checking = ref(false)
/** 本面板正式提交状态。 */
const committing = ref(false)
/** 预检查请求失败原因。 */
const checkError = ref('')
/** 仅通过预检查才能打开最终确认。 */
const confirmOpen = ref(false)
/** 用户对替换正式资产的明确确认。 */
const acknowledged = ref(false)
/** 已收到成功回执的版本,服务端刷新前也不允许重复写入。 */
const submittedId = ref('')
/** 过期、关闭和卸载时取消只读预检查。 */
let controller: AbortController | undefined
/** 拒绝忽略取消信号后返回的旧响应。 */
let version = 0
/** 当前候选生成或提交后的面板。 */
const isCandidate = computed(() => props.state?.candidateOnly === true && (props.quality.isCandidate || props.quality.committed))
/** 后端持久化标记优先,成功回执只在当前会话补充防重复锁。 */
const committed = computed(() => props.quality.committed || submittedId.value === `${props.projectId}:${props.checkpointId}`)
/** 审核通过后只允许请求预检查,绝不直接允许写入。 */
const canCheck = computed(() => !props.disabled && !operation.value.pending && !committed.value && props.quality.canRequestCommitPreview && props.quality.checkpointId === props.checkpointId)
/** 逐项与版本确认后的预检查结果。 */
const ready = computed(() => candidatePreviewReady(preview.value, props.projectId, props.checkpointId))
/** 仅展示具体未通过的安全检查。 */
const failedChecks = computed(() => preview.value ? candidateCheckKeys.filter(key => preview.value!.checks?.[key] !== true).map(key => candidateCheckLabels[key]) : [])
/** 当前候选规模与正式资产区别标注。 */
const counts = computed(() => ({ subjects: props.state?.subjectCandidates?.length ?? 0, episodes: props.state?.storyboardEpisodeShots?.length ?? 0, shots: props.state?.storyboardEpisodeShots?.reduce((sum, episode) => sum + episode.beatShots.reduce((total, beat) => total + beat.shots.length, 0), 0) ?? 0 }))
/** 撤销旧检查与确认,版本变化后不能复用提交许可。 */
function invalidate() {
version++
controller?.abort()
checking.value = false
preview.value = null
confirmOpen.value = false
acknowledged.value = false
checkError.value = ''
}
watch([() => props.projectId, () => props.checkpointId, () => props.quality.canRequestCommitPreview, () => props.quality.committed], invalidate, { flush: 'sync' })
watch(() => props.disabled, disabled => { if (disabled && !committing.value) invalidate() })
onScopeDispose(invalidate)
/** 安全检查通过后才展示最终确认;不调用任何写接口。 */
async function check() {
if (!canCheck.value || checking.value) return
invalidate()
const current = version
const id = props.projectId
const checkpointId = props.checkpointId
controller = new AbortController()
checking.value = true
try {
const result = await breakdownApi.candidateCommitPreview(id, controller.signal)
if (current !== version) return
if (result.projectId !== id || result.checkpointId !== checkpointId || result.readOnly !== true) throw new Error('候选安全预检查已过期,请刷新后重新检查。')
preview.value = result
if (candidatePreviewReady(result, id, checkpointId)) confirmOpen.value = true
else showActionFeedback(message, '候选暂不能提交,请处理未通过的安全检查。', 'warning')
} catch (cause) {
if (current === version) {
checkError.value = errorMessage(cause)
showActionFeedback(message, checkError.value, 'error')
}
} finally { if (current === version) checking.value = false }
}
/** 最终确认时重新核对版本和全部安全门禁,随后交由后端事务提交。 */
async function commit() {
if (!canCheck.value || !ready.value || !confirmOpen.value || !acknowledged.value || committing.value) return
const id = props.projectId
const checkpointId = props.checkpointId
const checkedVersion = version
committing.value = true
const ok = await runOperation(id, '正在提交拆解候选', async () => {
const records = await projectsApi.checkpoints(id)
const latest = workflowCheckpoints(records, 'breakdown').at(-1)
if (!checkpointId || latest?.checkpointId !== checkpointId || latest.state.storyboardPersisted || latest.state.candidateCommitted || props.projectId !== id || props.checkpointId !== checkpointId) throw new Error('拆解候选版本已变化或已经提交,请刷新后重新确认。')
const checked = await breakdownApi.candidateCommitPreview(id)
if (checkedVersion !== version || !props.quality.canRequestCommitPreview || !candidatePreviewReady(checked, id, checkpointId) || props.projectId !== id || props.checkpointId !== checkpointId) throw new Error('提交安全条件已变化,请重新检查候选。')
const receipt = await breakdownApi.commitCandidate(id)
if (receipt.projectId !== id) throw new Error('提交回执所属项目不一致,请刷新确认。')
return receipt
}, receipt => ({ type: 'success', content: `候选已提交:${receipt.subjectCount} 个主体、${receipt.episodeCount} 集、${receipt.shotCount} 个镜头。` }))
if (props.projectId === id) {
if (ok) submittedId.value = `${id}:${checkpointId}`
invalidate()
emit('refresh')
}
committing.value = false
}
</script>
<template>
<!-- 以下是候选质量与安全提交模块,不使用只读审核结果替代事务门禁 -->
<section v-if="isCandidate" class="workspace-panel space-y-4" aria-label="拆解候选">
<div class="flex flex-wrap items-center justify-between gap-3">
<h3 class="text-sm font-medium">{{ committed ? '候选已提交' : '待确认的拆解候选' }}</h3>
<span class="text-xs text-muted">{{ committed ? '正式主体' : '候选主体' }} {{ counts.subjects }} · {{ counts.episodes }} 集 · {{ counts.shots }} 镜</span>
</div>
<p class="text-xs leading-6 text-muted">{{ committed ? '这份结果已写入正式主体、形态与分镜。' : '候选尚未写入正式资产,质量审核后还需通过安全预检查。' }}</p>
<p v-if="!committed" class="text-xs text-muted" role="status">{{ quality.loading ? '正在重新审核当前候选…' : quality.needsRefresh ? '当前候选审核尚未确认,请刷新候选状态。' : quality.canRequestCommitPreview ? '候选质量审核通过,可执行提交安全预检查。' : '候选仍有待处理问题,请先修订。' }}</p>
<div class="flex flex-wrap items-center gap-3">
<NButton v-if="!committed" :disabled="!canCheck" :loading="checking || committing" @click="check">提交候选为正式结果</NButton>
<WorkspaceRefreshButton label="刷新候选状态" :loading="quality.loading" :disabled="operation.pending || checking" @refresh="emit('refresh')" />
</div>
<section v-if="failedChecks.length" class="problem-surface p-4" aria-label="未通过的提交安全检查"><p class="mb-2 text-sm">提交安全检查未通过</p><ul class="list-inside list-disc space-y-2 text-xs"><li v-for="label in failedChecks" :key="label">{{ label }}</li></ul></section>
<p v-if="checkError" class="text-xs text-danger" role="alert">{{ checkError }}</p>
<!-- 以下是通过只读预检查后才出现的最终确认,取消和关闭撤销确认 -->
<AppDialog v-model:open="confirmOpen" title="确认提交候选为正式结果" :busy="committing" description="将替换正式主体、形态与分镜。提交时后端仍会在事务内重新检查剧本版本和下游资产。">
<p class="text-sm">本次候选:{{ counts.subjects }} 个主体、{{ counts.episodes }} 集、{{ counts.shots }} 个镜头。</p>
<NCheckbox v-model:checked="acknowledged" class="mt-5">我已确认替换正式结果,当前没有同项目的制作任务在运行。</NCheckbox>
<template #footer><NButton :disabled="committing" @click="confirmOpen = false; acknowledged = false">取消</NButton><NButton type="primary" :loading="committing" :disabled="!acknowledged || !ready || !canCheck" @click="commit">确认提交候选为正式结果</NButton></template>
</AppDialog>
</section>
</template>