feat(short-drama-agent-front): 接入人工动作修订与视频版本权限

This commit is contained in:
GJ committed 2026-10-14 13:44:02 +08:00
1 parent 7a60db3ced
commit fa76165960
38 files changed
+2003 -158

No files matched your search

@@ -0,0 +1,352 @@
import { computed, ref, watch, onScopeDispose, type Ref } from 'vue'
import { usePolling } from '../../composables/usePolling'
import { motionApi } from './motion-api'
import { productionApi } from '../production/api'
import { useWorkflowExecution } from '../workflows/execution'
import { getOperation, runOperation } from '../workflows/operations'
import { ApiError, errorMessage } from '../../lib/http'
import { motionEvidenceCurrent, type MotionPlan, type MotionRevisionPreview } from './motion-revision'
/**
* 管理一个镜头的完整草稿、免费预检、保存和付费视觉任务;切镜后丢弃迟到 UI 更新。
* @param props 当前镜头与项目
* @param open 编辑面板可见状态
* @param changed 激活成功后通知外层刷新资产和就绪状态
* @returns 表单状态与明确操作入口
*/
export function useMotionRevision(
props: { projectId: string; shotId: string; disabled?: boolean },
open: Ref<boolean>,
changed: () => void
) {
/** 任务观察按项目隔离,真实状态来自持久化接口。 */
const project = computed(() => props.projectId)
const execution = useWorkflowExecution(project, 'motion-plan-start-state-validation')
/** 原目标身份同时用于废弃迟到响应。 */
const identity = computed(() => JSON.stringify([props.projectId, props.shotId]))
/** 面板关闭后不发起普通查询;活动任务仍由任务观察器恢复。 */
const key = computed(() => (open.value ? identity.value : ''))
/** 查询期间显示已有后端数据,但出错时禁用所有写入。 */
const context = usePolling(
key,
async (value, signal) => {
if (!value) return null
const [, shotId] = JSON.parse(value) as [string, string]
const [history, preview, keyframes] = await Promise.all([
motionApi.revisions(shotId, signal),
motionApi.preview(shotId, signal),
productionApi.listKeyframes(shotId, signal)
])
if (
history.shotId !== shotId ||
preview.shotId !== shotId ||
keyframes.some(item => item.shotId !== shotId)
)
throw new Error('动作修订或首帧与当前镜头不匹配。')
return {
history,
preview,
primary:
keyframes.find(
item => item.isPrimary && item.status === 'completed' && item.source !== 'provider_variant'
) ?? null
}
},
computed(() => (open.value ? 5000 : false))
)
/** 当前选择的已保存版本;空值表示从历史 AI 预览开始。 */
const selectedId = ref('')
/** 全字段动作草稿,不允许自动生成占位阶段。 */
const draft = ref<MotionPlan>({ beats: [], globalMotionConstraints: [] })
/** 人工修改原因。 */
const reason = ref('')
/** 只保留当前草稿的免费预检查。 */
const preview = ref<MotionRevisionPreview | null>(null)
/** 明确确认视觉模型费用。 */
const acknowledged = ref(false)
/** 草稿或输入变化后不再认可旧视觉证据。 */
const invalidated = ref(false)
/** 本次请求错误独立于只读查询。 */
const error = ref('')
/** 激活成功的提示词更新说明。 */
const notice = ref('')
/** 拷贝历史时不误判为用户修改。 */
let copying = false
/** 卸载后不更新当前界面。 */
let disposed = false
onScopeDispose(() => {
disposed = true
})
/** 当前后端保存版本,不以草稿代替不可变修订。 */
const revision = computed(
() => context.data.value?.history.revisions.find(item => item.revisionId === selectedId.value) ?? null
)
/** 草稿与所选修订内容保持一致才可校验或激活。 */
const saved = computed(
() => !!revision.value && JSON.stringify(draft.value) === JSON.stringify(revision.value.motionPlan)
)
/** 项目已有请求或后台任务时禁止重复发起。 */
const pending = computed(() => getOperation(props.projectId).pending)
/** 自动核对输入期间允许继续填写草稿,不让定时查询打断输入焦点。 */
const formBlocked = computed(
() => !!props.disabled || pending.value || !!context.error.value || !context.data.value
)
/** 后端查询缺失或失败时不使用旧数据提交。 */
const blocked = computed(
() => !!props.disabled || pending.value || context.loading.value || !!context.error.value || !context.data.value
)
/** 当前任务只展示本镜头记录,其他镜头任务不充当证据。 */
const record = computed(() => {
const item = execution.query.data.value
return item?.metadata?.shotId === props.shotId || item?.result?.shotId === props.shotId ? item : null
})
/** 完成、通过、未失效且所有绑定匹配才开放激活。 */
const evidenceValid = computed(
() =>
saved.value &&
!invalidated.value &&
motionEvidenceCurrent(
record.value,
revision.value,
context.data.value?.primary ?? null,
context.data.value?.history.activeRevisionId ?? null
)
)
/** 查询与请求进行中临时禁用提交,不把有效证据误显示为失效。 */
const canActivate = computed(() => !blocked.value && evidenceValid.value)
/** 当前版本和主首帧输入变化也撤销费用确认及旧证据。 */
const binding = computed(() =>
JSON.stringify([
context.data.value?.history.activeRevisionId ?? null,
context.data.value?.primary?.id ?? null,
context.data.value?.primary?.imageUrl ?? null
])
)
watch(binding, (_value, old) => {
if (old && old !== '[null,null,null]') invalidated.value = true
acknowledged.value = false
})
watch(
draft,
() => {
preview.value = null
acknowledged.value = false
if (!copying) invalidated.value = true
},
{ deep: true, flush: 'sync' }
)
watch([identity, open], () => {
selectedId.value = ''
draft.value = { beats: [], globalMotionConstraints: [] }
preview.value = null
reason.value = ''
error.value = ''
notice.value = ''
acknowledged.value = false
})
/**
* 复制所选版本或真实 AI 规划;改变来源后须重新取得视觉证据。
* @param id 所选版本 ID,空值使用预览
* @param initial 首次恢复允许核对已持久化任务
*/
function select(id: string, initial = false) {
const value = context.data.value
if (!value) return
const source =
value.history.revisions.find(item => item.revisionId === id)?.motionPlan ??
(id ? null : { beats: value.preview.beats, globalMotionConstraints: value.preview.globalMotionConstraints })
if (!source) return
copying = true
selectedId.value = id
draft.value = JSON.parse(JSON.stringify(source)) as MotionPlan
copying = false
preview.value = null
reason.value = ''
acknowledged.value = false
invalidated.value = !initial
notice.value = ''
}
watch(
() => context.data.value,
value => {
if (value && !selectedId.value && draft.value.beats.length === 0)
select(
value.history.activeRevisionId ??
value.history.originalRevisionId ??
value.history.revisions.at(-1)?.revisionId ??
'',
true
)
}
)
/** 刷新或重开时恢复同镜头的真实任务版本;用户修改后不恢复旧资格。 */
watch([() => context.data.value, record], ([value, task]) => {
const id = task?.metadata?.revisionId
if (
value &&
typeof id === 'string' &&
!invalidated.value &&
!preview.value &&
value.history.revisions.some(item => item.revisionId === id) &&
selectedId.value !== id &&
(saved.value || draft.value.beats.length === 0)
)
select(id, true)
})
/**
* 发起一次明确操作;冲突后刷新绑定,成功也校验响应目标。
* @param label 操作说明
* @param action 固定原镜头操作
*/
async function perform(label: string, action: () => Promise<void>) {
if (blocked.value) return
const target = identity.value
error.value = ''
notice.value = ''
await runOperation(props.projectId, label, async () => {
try {
await action()
} catch (cause) {
if (!disposed && identity.value === target) {
error.value = errorMessage(cause)
if (cause instanceof ApiError && cause.status === 409) {
invalidated.value = true
acknowledged.value = false
await context.refresh()
changed()
}
}
throw cause
}
})
}
/** 免费检查本次完整草稿;编辑或切镜后丢弃迟到回执。 */
async function precheck() {
const shotId = props.shotId
const plan = JSON.parse(JSON.stringify(draft.value)) as MotionPlan
const signature = JSON.stringify(plan)
await perform('免费检查动作修订', async () => {
const result = await motionApi.previewRevision(shotId, plan)
if (!disposed && props.shotId === shotId && signature === JSON.stringify(draft.value)) {
if (result.shotId !== shotId) throw new Error('预检查回执与镜头不匹配。')
preview.value = result
}
})
}
/** 保存必须先获得当前草稿的结构通过结果,不激活或调用模型。 */
async function save() {
if (preview.value?.valid !== true) return
const target = identity.value
const shotId = props.shotId
const plan = JSON.parse(JSON.stringify(draft.value)) as MotionPlan
const parentRevisionId = selectedId.value || null
await perform('保存动作修订', async () => {
const result = await motionApi.saveRevision(shotId, {
motionPlan: plan,
parentRevisionId,
reason: reason.value.trim() || null
})
if (result.shotId !== shotId || result.source !== 'manual' || result.isActive || !result.revisionId)
throw new Error('保存回执不是当前镜头的未生效人工版本。')
if (disposed || identity.value !== target) return
await context.refresh()
select(result.revisionId)
notice.value = '修订已保存,尚未生效;请确认模型费用后校验。'
})
}
/** 用户确认费用后重新读取首帧与生效指针,再启动视觉模型。 */
async function validate() {
if (!acknowledged.value || !saved.value || !revision.value || !context.data.value?.primary) return
const target = identity.value
const selected = revision.value
const originalProjectId = props.projectId
const expectedBinding = binding.value
acknowledged.value = false
await perform('校验动作修订起点', async () => {
await context.refresh()
if (
disposed ||
identity.value !== target ||
context.error.value ||
expectedBinding !== binding.value ||
!saved.value
)
throw new Error('主首帧或生效版本已变化,请刷新并重新确认费用。')
const primary = context.data.value?.primary
if (!primary?.imageUrl) throw new Error('尚无有效主首帧,不能启动视觉校验。')
const result = await motionApi.validateRevision(selected.shotId, selected.revisionId, {
primaryKeyframeId: primary.id,
expectedRevisionId: context.data.value!.history.activeRevisionId
})
if (!result.executionId || result.willCallModel !== true)
throw new Error('未返回有效视觉校验任务,请刷新,勿重复提交。')
if (!disposed && identity.value === target) invalidated.value = false
await execution.track(result.executionId, originalProjectId)
})
}
/** 激活前再次刷新并核对全部证据,成功后通知两个工作区同步。 */
async function activate() {
if (!canActivate.value || !revision.value || !record.value) return
const target = identity.value
const selected = revision.value
const taskId = record.value.id
await perform('激活动作修订', async () => {
await context.refresh()
if (
disposed ||
identity.value !== target ||
context.error.value ||
!saved.value ||
invalidated.value ||
!motionEvidenceCurrent(
record.value,
selected,
context.data.value?.primary ?? null,
context.data.value?.history.activeRevisionId ?? null
)
)
throw new Error('视觉证据已失效,请重新校验当前修订。')
const result = await motionApi.activateRevision(props.shotId, selected.revisionId, {
expectedRevisionId: context.data.value!.history.activeRevisionId,
validationExecutionId: taskId
})
if (
result.shotId !== selected.shotId ||
result.activeRevisionId !== selected.revisionId ||
result.motionPlanFingerprint !== selected.fingerprint
)
throw new Error('激活回执与目标版本不匹配,请刷新核对。')
if (disposed || identity.value !== target) return
await context.refresh()
changed()
notice.value = result.videoPromptStale
? '修订已生效。请重新生成视频提示词;旧视频候选仍保留,未自动提交生成任务。'
: '此版本已生效。'
})
}
return {
context,
selectedId,
draft,
reason,
preview,
acknowledged,
error,
notice,
revision,
saved,
pending,
blocked,
formBlocked,
record,
execution,
canActivate,
evidenceValid,
invalidated,
select,
precheck,
save,
validate,
activate
}
}