351 lines
15 KiB
TypeScript
351 lines
15 KiB
TypeScript
import { computed, onScopeDispose, ref, watch } from 'vue'
|
|
import { projectsApi } from '../projects/api'
|
|
import { useProjectContext } from '../projects/context'
|
|
import { subjectImagesApi } from '../subject-images/api'
|
|
import { hasRunningImages } from '../subject-images/model'
|
|
import { getOperation, runOperation } from '../workflows/operations'
|
|
import { useWorkflowExecution } from '../workflows/execution'
|
|
import { hasRunningWorkflow } from '../workflows/selectors'
|
|
import { ApiError, errorMessage } from '../../lib/http'
|
|
import { generationConfigApi } from '../generation-config/api'
|
|
import { productionApi } from './api'
|
|
import { getProductionSession } from './model'
|
|
import type { ProductionPlan, ProductionStartInput, ProductionStage } from './types'
|
|
|
|
/**
|
|
* 指定续跑阶段会跳过上游生成,必须使用已有有效资产而非可生成数量。
|
|
* @param plan 后端当前真实生产计划
|
|
* @param scope 用户选择的本轮执行范围
|
|
* @returns 与后端续跑门禁一致的阻断原因
|
|
*/
|
|
export function productionResumeIssues(
|
|
plan: Pick<ProductionPlan, 'characterIdentity' | 'subjectImages' | 'keyframes'>,
|
|
scope: ProductionStartInput
|
|
): string[] {
|
|
if (!scope.startFrom) return []
|
|
const issues: string[] = []
|
|
const identity = plan.characterIdentity
|
|
if (!identity || identity.ready !== identity.total) issues.push('指定阶段续跑前,需要全部角色身份确认并锁定。')
|
|
if (
|
|
(scope.startFrom === 'keyframes' || scope.startFrom === 'videoPrompts') &&
|
|
(!plan.subjectImages.total || plan.subjectImages.skipped !== plan.subjectImages.total)
|
|
)
|
|
issues.push('指定阶段续跑前,需要全部主体参考图有效且未过期。')
|
|
if (
|
|
scope.startFrom === 'videoPrompts' &&
|
|
(!plan.keyframes.total || plan.keyframes.skipped !== plan.keyframes.total)
|
|
)
|
|
issues.push('从视频提示词续跑前,需要全部有效首帧就绪。')
|
|
return issues
|
|
}
|
|
|
|
/**
|
|
* 使用后端计划区分可自动补齐和人工阻塞,保留项目归属及活动任务检查。
|
|
* @param projectId 当前项目 ID
|
|
* @param scope 已选择的生产范围
|
|
* @returns 无消耗计划、实际状态与同源请求参数
|
|
*/
|
|
export async function checkPipeline(projectId: string, scope: ProductionStartInput = {}) {
|
|
const profile = await generationConfigApi.profile(projectId)
|
|
const providers = profile
|
|
? {
|
|
imageProvider: profile.imageProvider,
|
|
videoProvider: profile.videoProvider,
|
|
imageGenerationOptions: { ...profile.imageOptions },
|
|
videoGenerationOptions: { ...profile.videoOptions }
|
|
}
|
|
: undefined
|
|
const [project, checkpoints, forms, plan, status] = await Promise.all([
|
|
projectsApi.detail(projectId),
|
|
projectsApi.checkpoints(projectId),
|
|
subjectImagesApi.listForms(projectId),
|
|
productionApi.plan(projectId, undefined, providers),
|
|
productionApi.status(projectId)
|
|
])
|
|
if (
|
|
project.id !== projectId ||
|
|
plan.projectId !== projectId ||
|
|
status.projectId !== projectId ||
|
|
forms.some(form => form.subject.projectId !== projectId)
|
|
)
|
|
throw new Error('预检返回了其他项目的数据,请重新读取。')
|
|
const issues: string[] = []
|
|
if (plan.entryReadiness && !plan.entryReadiness.ready)
|
|
issues.push(plan.entryReadiness.reason || '项目尚未满足生产入口条件。')
|
|
issues.push(...productionResumeIssues(plan, scope))
|
|
if (!profile) issues.push('尚未配置项目画布与生成模型,请先在右上角“项目配置”中保存配置。')
|
|
const shotIds = new Set(plan.details.keyframes.map(item => item.shotId))
|
|
if (
|
|
shotIds.size !== plan.keyframes.total ||
|
|
plan.details.keyframes.length !== plan.keyframes.total ||
|
|
plan.subjectImages.total !== forms.length ||
|
|
plan.details.subjectImages.length !== forms.length ||
|
|
plan.details.subjectImages.some(item => !forms.some(form => form.id === item.subjectFormId)) ||
|
|
[plan.details.videoPrompts, plan.details.videos, status.keyframes.items, status.videos.items].some(
|
|
items =>
|
|
items.length !== shotIds.size ||
|
|
new Set(items.map(item => item.shotId)).size !== shotIds.size ||
|
|
items.some(item => !shotIds.has(item.shotId))
|
|
)
|
|
)
|
|
issues.push('各阶段资产列表不一致,可能正在重新拆解,请刷新后重试。')
|
|
if (project.status !== 'completed') issues.push('剧本尚未完成,请先完成剧本创作。')
|
|
if (hasRunningWorkflow(checkpoints)) issues.push('项目仍有后台任务或工作流进行中。')
|
|
if (!forms.length || !plan.keyframes.total) issues.push('尚无正式形态或镜头,请先完成拆解和分镜。')
|
|
if (forms.some(form => hasRunningImages(form.images))) issues.push('形态图片仍在生成中。')
|
|
if (
|
|
status.videos.inProgress ||
|
|
status.videos.items.some(item => item.status === 'in_progress' || item.activeVideoId)
|
|
)
|
|
issues.push('存在活动视频任务,请等待结束后再操作。')
|
|
if (status.completed || plan.completed) issues.push('当前项目制作已完成,无需再次启动。')
|
|
else if (![plan.subjectImages, plan.keyframes, plan.videoPrompts, plan.videos].some(stage => stage.planned > 0))
|
|
issues.push('当前没有可执行的制作任务,请先处理计划中的阻塞项。')
|
|
// 人工阻塞在计划中独立展示;不拦截其余 ready 形态的部分成功生产。
|
|
return {
|
|
projectId,
|
|
issues,
|
|
plan,
|
|
status,
|
|
profile,
|
|
shotCount: plan.keyframes.total,
|
|
input: { ...scope, ...providers } as ProductionStartInput,
|
|
checkedAt: new Date().toISOString()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 预检不付费;确认后再次预检并锁住本浏览器项目,长请求不自动重发。
|
|
* @returns 生产范围、预检状态及一次性提交操作
|
|
*/
|
|
export function useAdvancedProduction() {
|
|
const context = useProjectContext()
|
|
/** 当前项目 ID。 */
|
|
const id = computed(() => context.project.value?.id ?? '')
|
|
/** 项目级操作锁。 */
|
|
const operation = computed(() => getOperation(id.value))
|
|
/** 项目生产回执。 */
|
|
const session = computed(() => getProductionSession(id.value))
|
|
const execution = useWorkflowExecution(id, 'production')
|
|
/** 正在读取预检。 */
|
|
const checking = ref(false)
|
|
/** 预检错误或已有任务说明。 */
|
|
const error = ref('')
|
|
/** 默认使用无视频测试模式,只有用户明确切换后才允许提交高费用视频任务。 */
|
|
const safeMode = ref(true)
|
|
/** 本轮有限首帧数量,保留原安全模式字段。 */
|
|
const testKeyframeLimit = ref<number | null>(1)
|
|
/** 恢复起点;留空从默认入口开始。 */
|
|
const startFrom = ref<ProductionStage | null>(null)
|
|
/** 显式结束阶段;安全模式下为空时默认在视频提示词后停止。 */
|
|
const stopAfter = ref<ProductionStage | null>(null)
|
|
/** 本轮稳定身份母版上限。 */
|
|
const stableIdentityAnchorLimit = ref<number | null>(1)
|
|
/** 本轮主体图片上限。 */
|
|
const subjectImageLimit = ref<number | null>(1)
|
|
/** 本轮视频提示词上限。 */
|
|
const videoPromptLimit = ref<number | null>(1)
|
|
/** 本轮视频候选镜头上限,默认只处理一镜。 */
|
|
const videoLimit = ref<number | null>(1)
|
|
/** 可供界面和确认描述共用的生产范围。 */
|
|
const scope = computed<ProductionStartInput>(() => ({
|
|
...(startFrom.value ? { startFrom: startFrom.value } : {}),
|
|
...(stopAfter.value || safeMode.value ? { stopAfter: stopAfter.value ?? 'videoPrompts' } : {}),
|
|
stableIdentityAnchorLimit: stableIdentityAnchorLimit.value ?? 0,
|
|
subjectImageLimit: subjectImageLimit.value ?? 0,
|
|
keyframeLimit: testKeyframeLimit.value ?? 0,
|
|
videoPromptLimit: videoPromptLimit.value ?? 0,
|
|
videoLimit: videoLimit.value ?? 0
|
|
}))
|
|
/** 阶段顺序与后端正整数约束。 */
|
|
const scopeError = computed(() => validateProductionScope(scope.value))
|
|
/** 已读取的预检与其不可变请求快照。 */
|
|
const check = ref<Awaited<ReturnType<typeof checkPipeline>> | null>(null)
|
|
/** 当前项目不可提交条件。 */
|
|
const blocked = computed(
|
|
() =>
|
|
!id.value ||
|
|
operation.value.pending ||
|
|
execution.active.value ||
|
|
!!context.error.value ||
|
|
context.project.value?.status !== 'completed'
|
|
)
|
|
let version = 0
|
|
let disposed = false
|
|
onScopeDispose(() => {
|
|
disposed = true
|
|
version++
|
|
})
|
|
watch(id, () => {
|
|
version++
|
|
check.value = null
|
|
error.value = ''
|
|
checking.value = false
|
|
})
|
|
|
|
watch(
|
|
[scope, safeMode],
|
|
() => {
|
|
version++
|
|
check.value = null
|
|
checking.value = false
|
|
error.value = ''
|
|
},
|
|
{ deep: true, flush: 'sync' }
|
|
)
|
|
|
|
/** 只读取无消耗计划,保存此次范围与模型参数。 */
|
|
async function preflight() {
|
|
if (blocked.value || checking.value || scopeError.value) return
|
|
const current = ++version
|
|
const projectId = id.value
|
|
checking.value = true
|
|
check.value = null
|
|
error.value = ''
|
|
try {
|
|
const result = await checkPipeline(projectId, scope.value)
|
|
if (current === version && id.value === projectId) check.value = result
|
|
} catch (cause) {
|
|
if (current === version) error.value = errorMessage(cause)
|
|
} finally {
|
|
if (current === version) checking.value = false
|
|
}
|
|
}
|
|
|
|
/** 确认后校验参数快照并提交一次;冲突时观察现有任务。 */
|
|
async function start() {
|
|
if (
|
|
blocked.value ||
|
|
!!scopeError.value ||
|
|
checking.value ||
|
|
!check.value ||
|
|
check.value.projectId !== id.value ||
|
|
check.value.issues.length
|
|
)
|
|
return
|
|
const projectId = id.value
|
|
const target = getProductionSession(projectId)
|
|
const confirmedInput = JSON.stringify(check.value.input)
|
|
const confirmedVersion = version
|
|
let submitted = false
|
|
await runOperation(projectId, '高级串联生产', async () => {
|
|
const result = await checkPipeline(projectId, scope.value)
|
|
if (
|
|
confirmedVersion !== version ||
|
|
disposed ||
|
|
id.value !== projectId ||
|
|
context.project.value?.status !== 'completed' ||
|
|
context.error.value
|
|
)
|
|
return
|
|
if (JSON.stringify(result.input) !== confirmedInput) {
|
|
check.value = null
|
|
throw new Error('项目模型或图片、视频参数已变化,请重新预检并确认费用。')
|
|
}
|
|
check.value = result
|
|
if (result.issues.length) throw new Error('提交前预检发现条件变化,未启动生产。请处理下方问题。')
|
|
target.pipelineReceipt = null
|
|
submitted = true
|
|
let started
|
|
try {
|
|
started = await productionApi.startPipeline(projectId, result.input)
|
|
} catch (cause) {
|
|
if (
|
|
cause instanceof ApiError &&
|
|
cause.status === 409 &&
|
|
cause.details &&
|
|
typeof cause.details === 'object' &&
|
|
'executionId' in cause.details &&
|
|
typeof cause.details.executionId === 'string' &&
|
|
cause.details.executionId
|
|
) {
|
|
if (!disposed && id.value === projectId)
|
|
error.value = '已有任务:已附着当前生产后台任务,请等待其结束。'
|
|
await execution.track(cause.details.executionId, projectId)
|
|
return
|
|
}
|
|
throw cause
|
|
}
|
|
if (!started.executionId) throw new Error('未取得制作任务编号,请先刷新状态,避免重复提交。')
|
|
await execution.track(started.executionId, projectId)
|
|
})
|
|
if (!disposed && id.value === projectId) {
|
|
if (submitted) check.value = null
|
|
await context.refresh()
|
|
}
|
|
}
|
|
watch(
|
|
() => execution.query.data.value?.status,
|
|
async (status, previous) => {
|
|
if ((status === 'completed' || status === 'failed') && status !== previous) {
|
|
const result = execution.query.data.value?.result
|
|
if (status === 'completed' && result && typeof result === 'object') {
|
|
targetPipelineReceipt(id.value, result, session.value)
|
|
}
|
|
await context.refresh()
|
|
}
|
|
}
|
|
)
|
|
|
|
return {
|
|
id,
|
|
operation,
|
|
session,
|
|
execution,
|
|
checking,
|
|
error,
|
|
check,
|
|
blocked,
|
|
safeMode,
|
|
testKeyframeLimit,
|
|
startFrom,
|
|
stopAfter,
|
|
stableIdentityAnchorLimit,
|
|
subjectImageLimit,
|
|
videoPromptLimit,
|
|
videoLimit,
|
|
scope,
|
|
scopeError,
|
|
preflight,
|
|
start
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 完成任务结果只在项目归属正确时保存为回执。
|
|
* @param projectId 当前项目 ID
|
|
* @param result 后端真实完成结果
|
|
* @param session 当前项目回执容器
|
|
*/
|
|
function targetPipelineReceipt(projectId: string, result: object, session: ReturnType<typeof getProductionSession>) {
|
|
if (!projectId || !('projectId' in result) || result.projectId !== projectId) return
|
|
session.pipelineReceipt = result as import('./types').ProductionPipelineResult
|
|
}
|
|
|
|
/**
|
|
* 验证后端阶段枚举、顺序和本轮生成上限。
|
|
* @param input 本轮启动范围
|
|
* @returns 可直接展示的阻断原因;空字符串表示有效
|
|
*/
|
|
export function validateProductionScope(input: ProductionStartInput): string {
|
|
const stages: ProductionStage[] = ['subjectImages', 'keyframes', 'videoPrompts']
|
|
if (
|
|
(input.startFrom && !stages.includes(input.startFrom)) ||
|
|
(input.stopAfter && !stages.includes(input.stopAfter))
|
|
)
|
|
return '请选择有效的生产阶段。'
|
|
if (input.startFrom && input.stopAfter && stages.indexOf(input.startFrom) > stages.indexOf(input.stopAfter))
|
|
return '结束阶段不能早于起始阶段。'
|
|
if (
|
|
[
|
|
input.stableIdentityAnchorLimit,
|
|
input.subjectImageLimit,
|
|
input.keyframeLimit,
|
|
input.videoPromptLimit,
|
|
input.videoLimit
|
|
].some(value => value !== undefined && (!Number.isInteger(value) || value <= 0))
|
|
)
|
|
return '所有生成上限必须是大于 0 的整数。'
|
|
return ''
|
|
}
|