741 lines
31 KiB
TypeScript
741 lines
31 KiB
TypeScript
import { imageValidationPassed } from '../subject-images/validation'
|
||
import type { ImageGenerationSubmission } from '../subject-images/generation-preview'
|
||
import { computed, ref, shallowRef, watch } from 'vue'
|
||
import { usePolling } from '../../composables/usePolling'
|
||
import { visualStyleApi } from '../visual-style'
|
||
import { useProjectGenerationRevision } from '../generation-config/context'
|
||
import { generationConfigApi } from '../generation-config/api'
|
||
import { subjectImagesApi } from '../subject-images/api'
|
||
import { hasRunningImages } from '../subject-images/model'
|
||
import { batchResultNotice, getOperation, runOperation, type OperationNotice } from '../workflows/operations'
|
||
import { useProjectMutationGuard } from '../workflows/useProjectMutationGuard'
|
||
import { useProjectContext } from '../projects/context'
|
||
import { subjectIdentityApi } from './api'
|
||
import { canBeAnchor, getIdentitySession, groupIdentitySubjects, mergeCastingSubjects } from './model'
|
||
import type {
|
||
IdentityImageOperation,
|
||
IdentityValidationResult,
|
||
StableAnchorQualityInput,
|
||
StableAnchorQualityResult,
|
||
GenerateCastingCandidateInput,
|
||
GenerateIdentityImageInput,
|
||
IdentityImage,
|
||
SaveIdentityInput,
|
||
SubjectIdentity
|
||
} from './types'
|
||
|
||
/** 单主体保存响应必须与提交时固定的正式 ID 一致。 */
|
||
function assertIdentity(value: SubjectIdentity, id: string) {
|
||
if (!value || value.subjectId !== id) throw new Error('没有获取到匹配的主体身份,请刷新后重试。')
|
||
}
|
||
|
||
/** 主体目录、项目风格和当前身份分开查询,未创建 Identity 不误报为空图库错误。 */
|
||
export function useSubjectIdentity() {
|
||
const { projectId, operation, blocked: projectBlocked } = useProjectMutationGuard()
|
||
const projectContext = useProjectContext()
|
||
const selectedId = ref('')
|
||
const editorRevision = ref(0)
|
||
const dirty = ref(false)
|
||
const concurrency = ref(3)
|
||
const candidateLimit = ref(3)
|
||
const force = ref(false)
|
||
/** 主体与模型查询只在进入、切换和自身操作完成后读取。 */
|
||
const generationModelsQuery = usePolling(
|
||
projectId,
|
||
async (_id, signal) => generationConfigApi.catalog(signal),
|
||
false
|
||
)
|
||
const catalog = usePolling(
|
||
projectId,
|
||
async (id, signal) => {
|
||
const forms = await subjectImagesApi.listForms(id, signal)
|
||
if (
|
||
forms.some(
|
||
form => form.subject.projectId !== id || form.images.some(image => image.subjectFormId !== form.id)
|
||
)
|
||
)
|
||
throw new Error('主体目录与当前项目不匹配,请刷新后重试。')
|
||
return {
|
||
subjects: groupIdentitySubjects(forms),
|
||
running: forms.some(form => hasRunningImages(form.images))
|
||
}
|
||
},
|
||
false
|
||
)
|
||
const styleQuery = usePolling(
|
||
projectId,
|
||
async (id, signal) => {
|
||
const style = await visualStyleApi.get(id, signal)
|
||
if (style && style.projectId !== id) throw new Error('视觉风格与当前项目不匹配。')
|
||
return { style }
|
||
},
|
||
false
|
||
)
|
||
/** 全局配置成功保存后重新读取主体生图的实际默认参数。 */
|
||
const generationRevision = useProjectGenerationRevision()
|
||
/** 配置查询按当前项目和修订号隔离。 */
|
||
const profileKey = computed(() => JSON.stringify([projectId.value, generationRevision.value]))
|
||
/** 主体身份使用保存后的正式项目配置。 */
|
||
const profileQuery = usePolling(
|
||
profileKey,
|
||
async (key, signal) => {
|
||
const [id] = JSON.parse(key) as [string, number]
|
||
return { profile: await generationConfigApi.profile(id, signal) }
|
||
},
|
||
false
|
||
)
|
||
const castingQuery = usePolling(
|
||
projectId,
|
||
async (id, signal) => {
|
||
const readiness = await subjectIdentityApi.castingReadiness(id, signal)
|
||
return readiness
|
||
},
|
||
false
|
||
)
|
||
const subjects = computed(() =>
|
||
mergeCastingSubjects(catalog.data.value?.subjects ?? [], castingQuery.data.value?.items ?? [], projectId.value)
|
||
)
|
||
const subject = computed(() => subjects.value.find(item => item.id === selectedId.value))
|
||
const selectionKey = computed(() => subject.value?.id ?? '')
|
||
/** 延后关联详情查询,使用响应式引用让任务轮询在查询建立后重新计算。 */
|
||
const detailRef = shallowRef<ReturnType<
|
||
typeof usePolling<{ identity: SubjectIdentity | null; images: IdentityImage[] }>
|
||
> | null>(null)
|
||
/** 仅当前主体存在未结束图片时轮询,终态后停止。 */
|
||
const detailPollInterval = computed(() =>
|
||
detailRef.value?.data.value?.images.some(image => image.status === 'pending' || image.status === 'generating')
|
||
? 4000
|
||
: false
|
||
)
|
||
const detail = usePolling(
|
||
selectionKey,
|
||
async (id, signal) => {
|
||
if (!id) return null
|
||
const identity = await subjectIdentityApi.get(id, signal)
|
||
if (!identity) return { identity: null, images: [] as IdentityImage[] }
|
||
assertIdentity(identity, id)
|
||
const images = await subjectIdentityApi.listImages(id, signal)
|
||
if (images.some(image => image.identityId !== identity.id)) throw new Error('身份图片与当前主体不匹配。')
|
||
return { identity, images }
|
||
},
|
||
detailPollInterval
|
||
)
|
||
detailRef.value = detail
|
||
/** 图片从生成中进入终态后同步母版、目录、选角和阶段准入,切换主体不消费旧结果。 */
|
||
watch(detail.data, (current, previous) => {
|
||
if (!current?.identity || !previous?.identity || current.identity.id !== previous.identity.id) return
|
||
if (current.identity.subjectId !== selectedId.value) return
|
||
const runningIds = new Set(
|
||
previous.images
|
||
.filter(image => image.status === 'pending' || image.status === 'generating')
|
||
.map(image => image.id)
|
||
)
|
||
if (
|
||
current.images.some(
|
||
image => runningIds.has(image.id) && (image.status === 'completed' || image.status === 'failed')
|
||
)
|
||
)
|
||
void refreshImageConfirmation(projectId.value, selectedId.value)
|
||
})
|
||
const identity = computed(() => detail.data.value?.identity ?? null)
|
||
const images = computed(() => detail.data.value?.images ?? [])
|
||
const castingItem = computed(() =>
|
||
castingQuery.data.value?.items?.find(item => item.subjectId === subject.value?.id)
|
||
)
|
||
const session = computed(() => getIdentitySession(projectId.value))
|
||
const blocked = computed(
|
||
() =>
|
||
projectBlocked.value ||
|
||
!!catalog.error.value ||
|
||
!catalog.data.value ||
|
||
!!styleQuery.error.value ||
|
||
!styleQuery.data.value
|
||
)
|
||
const detailBlocked = computed(
|
||
() =>
|
||
blocked.value ||
|
||
!subject.value ||
|
||
!detail.data.value ||
|
||
!!detail.error.value ||
|
||
hasRunningImages(images.value)
|
||
)
|
||
const hasStyle = computed(() => !!styleQuery.data.value?.style)
|
||
const hasCreativeProfile = computed(() => !!profileQuery.data.value?.profile)
|
||
const canGenerateText = computed(
|
||
() => !detailBlocked.value && hasStyle.value && !identity.value?.isLocked && !dirty.value
|
||
)
|
||
/** 图片入口允许先核对配置;任务、来源、正文和草稿限制仍然有效。 */
|
||
const canRequestImage = computed(
|
||
() =>
|
||
!detailBlocked.value &&
|
||
hasStyle.value &&
|
||
!!profileQuery.data.value &&
|
||
!profileQuery.error.value &&
|
||
!profileQuery.loading.value &&
|
||
!!identity.value?.generationPrompt?.trim() &&
|
||
!dirty.value
|
||
)
|
||
/** 真正生成仍必须具备正式保存的模型配置。 */
|
||
const canGenerateImage = computed(() => canRequestImage.value && hasCreativeProfile.value)
|
||
const activeIdentityGeneration = computed(() =>
|
||
images.value.find(image => image.status === 'pending' || image.status === 'generating')
|
||
)
|
||
/** 角色候选入口与正式生成共用选角和活动图片限制,缺配置时先引导。 */
|
||
const canRequestCandidate = computed(
|
||
() =>
|
||
canRequestImage.value &&
|
||
subject.value?.module === 'character' &&
|
||
!!castingQuery.data.value &&
|
||
!castingQuery.error.value &&
|
||
!activeIdentityGeneration.value
|
||
)
|
||
/** 角色图片写入不会因为入口可点击而绕过模型配置。 */
|
||
const canGenerateCandidate = computed(() => canRequestCandidate.value && hasCreativeProfile.value)
|
||
const batchValid = computed(() => Number.isSafeInteger(concurrency.value) && concurrency.value > 0)
|
||
const candidateLimitValid = computed(() => Number.isSafeInteger(candidateLimit.value) && candidateLimit.value > 0)
|
||
const castingBlocked = computed(
|
||
() => blocked.value || !!castingQuery.error.value || !castingQuery.data.value || dirty.value
|
||
)
|
||
|
||
watch(subjects, rows => {
|
||
if (!rows.some(item => item.id === selectedId.value)) selectedId.value = rows[0]?.id ?? ''
|
||
})
|
||
watch(selectionKey, () => {
|
||
dirty.value = false
|
||
editorRevision.value++
|
||
})
|
||
|
||
/** 校验结果以正式主体 ID 保存,切换主体不会串回执。 */
|
||
const validationResults = ref<Record<string, IdentityValidationResult>>({})
|
||
/** 质量闭环结果以正式主体 ID 保存。 */
|
||
const qualityResults = ref<Record<string, StableAnchorQualityResult>>({})
|
||
/** 审核只适用于本次主体、身份版本和图片输入。 */
|
||
const validationSourceKey = computed(() =>
|
||
JSON.stringify([projectId.value, selectedId.value, identity.value, images.value])
|
||
)
|
||
/** 来源变化或切换主体后清除旧审核展示,不能沿用历史通过。 */
|
||
watch(
|
||
validationSourceKey,
|
||
() => {
|
||
delete validationResults.value[selectedId.value]
|
||
},
|
||
{ flush: 'sync' }
|
||
)
|
||
/** 当前主体的指定图片校验结果。 */
|
||
const validationResult = computed(() => validationResults.value[selectedId.value] ?? null)
|
||
/** 当前主体的质量闭环回执。 */
|
||
const qualityResult = computed(() => qualityResults.value[selectedId.value] ?? null)
|
||
/** 固定图片请求目标,切换主体后不把旧请求的加载状态附在新图片上。 */
|
||
const pendingImageOperation = ref<IdentityImageOperation | null>(null)
|
||
/**
|
||
* 执行已确认的图片操作,并让加载状态持续到响应核对及刷新完成。
|
||
* @param kind 图片操作类型
|
||
* @param imageId 提交时固定的正式图片编号
|
||
* @param label 包含主体名称的操作提示
|
||
* @param action 提交请求并核验结果
|
||
* @param feedback 按实际结果显示的临时消息
|
||
* @returns 请求及结果核验是否成功
|
||
*/
|
||
async function runImageOperation<T>(
|
||
kind: IdentityImageOperation['kind'],
|
||
imageId: string,
|
||
label: string,
|
||
action: () => Promise<T>,
|
||
feedback: OperationNotice | ((result: T) => OperationNotice)
|
||
) {
|
||
if (operation.value.pending || !subject.value) return false
|
||
pendingImageOperation.value = { kind, imageId, subjectId: subject.value.id, projectId: projectId.value }
|
||
try {
|
||
return await runOperation(projectId.value, label, action, feedback)
|
||
} finally {
|
||
pendingImageOperation.value = null
|
||
}
|
||
}
|
||
/**
|
||
* 图片确认后刷新目录、选角与准入状态,仅更新仍在原主体的详情。
|
||
* @param project 请求提交时的项目编号
|
||
* @param subjectId 请求提交时的主体编号
|
||
*/
|
||
async function refreshImageConfirmation(project: string, subjectId: string) {
|
||
if (projectId.value !== project) return
|
||
await Promise.all([
|
||
catalog.refresh(),
|
||
castingQuery.refresh(),
|
||
projectContext.refreshStageReadiness(),
|
||
...(selectedId.value === subjectId ? [detail.refresh()] : [])
|
||
])
|
||
}
|
||
/**
|
||
* 校验当前主体的正式图片,费用确认由图库负责。
|
||
* @param imageId 指定正式身份图片 ID
|
||
*/
|
||
async function validateImage(imageId: string) {
|
||
const image = images.value.find(item => item.id === imageId)
|
||
if (detailBlocked.value || !subject.value || image?.status !== 'completed' || !image.imageUrl) return
|
||
const id = subject.value.id
|
||
const name = subject.value.name
|
||
delete validationResults.value[id]
|
||
const sourceKey = validationSourceKey.value
|
||
await runImageOperation(
|
||
'validate',
|
||
imageId,
|
||
`校验 ${name} 身份图片`,
|
||
async () => {
|
||
const result = await subjectIdentityApi.validateImage(id, imageId)
|
||
if (result.subjectId !== id || result.imageId !== imageId)
|
||
throw new Error('身份校验回执与提交目标不匹配')
|
||
if (validationSourceKey.value === sourceKey) validationResults.value[id] = result
|
||
return result
|
||
},
|
||
result => ({
|
||
type: imageValidationPassed(result) ? 'success' : 'warning',
|
||
content: `${name} 的身份图片校验${imageValidationPassed(result) ? '通过' : '未通过或待复核'}:${result.summary}`
|
||
})
|
||
)
|
||
}
|
||
/**
|
||
* 为场景或道具执行有限预算质量闭环,仅按真实回执确认锁定。
|
||
* @param input 模型、参数和修复次数
|
||
*/
|
||
async function generateStableAnchorQuality(input: StableAnchorQualityInput) {
|
||
if (
|
||
!canGenerateImage.value ||
|
||
!subject.value ||
|
||
!identity.value ||
|
||
!['scene', 'prop'].includes(subject.value.module)
|
||
)
|
||
return
|
||
if (!Number.isInteger(input.maxRepairAttempts) || input.maxRepairAttempts < 0 || input.maxRepairAttempts > 3)
|
||
return
|
||
const id = subject.value.id
|
||
const identityId = identity.value.id
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
delete qualityResults.value[id]
|
||
await runOperation(
|
||
project,
|
||
`生成 ${name} 稳定身份母版并校验质量`,
|
||
async () => {
|
||
const result = await subjectIdentityApi.generateStableAnchorQuality(id, input)
|
||
if (
|
||
result.passed &&
|
||
(!result.confirmed ||
|
||
result.confirmed.subjectId !== id ||
|
||
result.confirmed.identityId !== identityId ||
|
||
!result.confirmed.isLocked ||
|
||
!result.confirmed.anchor.isAnchor ||
|
||
result.confirmed.anchor.id !== result.imageId)
|
||
)
|
||
throw new Error('质量生成的母版与锁定回执未确认,请刷新核对')
|
||
qualityResults.value[id] = result
|
||
await refreshImageConfirmation(project, id)
|
||
return result
|
||
},
|
||
result => ({
|
||
type: result.passed ? 'success' : 'warning',
|
||
content: result.passed
|
||
? `${name} 的身份母版校验通过,已确认并锁定。`
|
||
: `${name} 的身份母版校验未通过,候选仍保留在图库。${result.stopReason || ''}`
|
||
})
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 保存或生成身份后刷新项目准入状态,跨主体响应不覆盖新主体详情。
|
||
* @param label 操作反馈名称
|
||
* @param action 对指定主体执行的身份写入请求
|
||
* @returns 身份写入与当前项目状态刷新完成的异步结果
|
||
*/
|
||
async function writeIdentity(label: string, action: (id: string) => Promise<SubjectIdentity>) {
|
||
if (detailBlocked.value || !subject.value) return
|
||
const id = subject.value.id
|
||
const project = projectId.value
|
||
const ok = await runOperation(project, label, async () => {
|
||
const result = await action(id)
|
||
assertIdentity(result, id)
|
||
if (selectionKey.value === id && projectId.value === project) {
|
||
// 图片的 isAnchor 只能从专用查询获取,保存文本时暂时保留已有图片列表。
|
||
detail.data.value = { identity: result, images: images.value }
|
||
editorRevision.value++
|
||
dirty.value = false
|
||
}
|
||
})
|
||
if (ok && projectId.value === project) {
|
||
await Promise.all([
|
||
castingQuery.refresh(),
|
||
projectContext.refreshStageReadiness(),
|
||
...(selectedId.value === id ? [detail.refresh()] : [])
|
||
])
|
||
}
|
||
}
|
||
|
||
/** 人工保存可修改已锁定身份;锁定只阻止自动重生成。 */
|
||
function save(input: SaveIdentityInput) {
|
||
return writeIdentity('保存主体身份', id => subjectIdentityApi.save(id, input))
|
||
}
|
||
|
||
/** 单主体 AI 生成与已存在文本的覆盖均需要页面确认。 */
|
||
function generate() {
|
||
if (!canGenerateText.value) return
|
||
const overwrite = !!identity.value
|
||
return writeIdentity('AI 生成主体身份', id => subjectIdentityApi.generate(id, overwrite))
|
||
}
|
||
|
||
/** 批量仅生成身份文本,已锁定项会自动跳过,回执保留部分失败。 */
|
||
async function generateProject(charactersOnly = false, module?: 'scene' | 'prop') {
|
||
if (
|
||
blocked.value ||
|
||
!!catalog.data.value?.running ||
|
||
!hasStyle.value ||
|
||
!batchValid.value ||
|
||
dirty.value ||
|
||
!subjects.value.length
|
||
)
|
||
return
|
||
const id = projectId.value
|
||
const target = getIdentitySession(id)
|
||
const input = { concurrency: concurrency.value, force: force.value, ...(module ? { module } : {}) }
|
||
target.receipt = null
|
||
const title = charactersOnly
|
||
? '批量生成角色身份文本'
|
||
: module
|
||
? `批量生成${module === 'scene' ? '场景' : '道具'}身份文本`
|
||
: '批量生成全部主体身份文本'
|
||
await runOperation(
|
||
id,
|
||
title,
|
||
async () => {
|
||
target.receipt = {
|
||
kind: 'identities',
|
||
title,
|
||
result: charactersOnly
|
||
? await subjectIdentityApi.generateCharacters(id, input)
|
||
: await subjectIdentityApi.generateProject(id, input)
|
||
}
|
||
return target.receipt.result
|
||
},
|
||
result => batchResultNotice(result, title)
|
||
)
|
||
await Promise.all([detail.refresh(), castingQuery.refresh()])
|
||
}
|
||
|
||
/** 场景/道具身份母版会独立生成、设为母版并锁定,保留逐主体失败回执。 */
|
||
async function generateStableAnchors(module: 'scene' | 'prop') {
|
||
if (
|
||
blocked.value ||
|
||
!!catalog.data.value?.running ||
|
||
!hasStyle.value ||
|
||
!hasCreativeProfile.value ||
|
||
!batchValid.value ||
|
||
!candidateLimitValid.value ||
|
||
dirty.value
|
||
)
|
||
return
|
||
const id = projectId.value
|
||
const target = getIdentitySession(id)
|
||
const title = `批量生成${module === 'scene' ? '场景' : '道具'}身份母版`
|
||
target.receipt = null
|
||
await runOperation(
|
||
id,
|
||
title,
|
||
async () => {
|
||
target.receipt = {
|
||
kind: 'stable-anchors',
|
||
title,
|
||
result: await subjectIdentityApi.generateStableAnchors(id, {
|
||
module,
|
||
limit: candidateLimit.value,
|
||
concurrency: concurrency.value
|
||
})
|
||
}
|
||
return target.receipt.result
|
||
},
|
||
result => batchResultNotice(result, title)
|
||
)
|
||
await Promise.all([catalog.refresh(), detail.refresh()])
|
||
}
|
||
|
||
/** 小批量只为 missing_anchor 的 Character 生成候选,不自动选择演员。 */
|
||
async function generateCastingCandidates() {
|
||
if (
|
||
castingBlocked.value ||
|
||
!!catalog.data.value?.running ||
|
||
!hasStyle.value ||
|
||
!hasCreativeProfile.value ||
|
||
!batchValid.value ||
|
||
!candidateLimitValid.value ||
|
||
!castingQuery.data.value?.missingAnchor
|
||
)
|
||
return
|
||
const id = projectId.value
|
||
const target = getIdentitySession(id)
|
||
const title = '批量生成角色选角候选'
|
||
target.receipt = null
|
||
await runOperation(
|
||
id,
|
||
title,
|
||
async () => {
|
||
target.receipt = {
|
||
kind: 'casting',
|
||
title,
|
||
result: await subjectIdentityApi.generateCastingCandidates(id, {
|
||
limit: candidateLimit.value,
|
||
concurrency: concurrency.value
|
||
})
|
||
}
|
||
return target.receipt.result
|
||
},
|
||
result => batchResultNotice(result, title)
|
||
)
|
||
await Promise.all([detail.refresh(), castingQuery.refresh()])
|
||
}
|
||
|
||
/**
|
||
* 生图不修改文字锁定;即使有自定义提示词,也需已有身份提示词与项目风格。
|
||
* @param input 已确认的视图、模型与参考图片参数
|
||
* @returns 任务创建结果及原始错误
|
||
*/
|
||
async function generateImage(input: GenerateIdentityImageInput): Promise<ImageGenerationSubmission> {
|
||
if (!canGenerateImage.value || !subject.value || !identity.value) return { accepted: false }
|
||
if (
|
||
input.referenceImageId &&
|
||
!images.value.some(
|
||
image => image.id === input.referenceImageId && image.status === 'completed' && image.imageUrl
|
||
)
|
||
)
|
||
return { accepted: false }
|
||
const id = subject.value.id
|
||
const identityId = identity.value.id
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
const accepted = await runOperation(
|
||
project,
|
||
`提交 ${name} 身份参考图生成`,
|
||
async () => {
|
||
const started = await subjectIdentityApi.generateImage(id, input)
|
||
const image = started?.image
|
||
if (
|
||
!started?.executionId ||
|
||
!image ||
|
||
image.identityId !== identityId ||
|
||
!['pending', 'generating'].includes(image.status)
|
||
) {
|
||
throw new Error(image?.error || '身份参考图任务未成功创建,请刷新后重试。')
|
||
}
|
||
if (projectId.value === project && selectedId.value === id) {
|
||
// 先展示真实回执中的生成记录,后续读取失败也能保留状态并继续轮询。
|
||
const current = detail.data.value
|
||
if (current?.identity?.id === identityId)
|
||
detail.data.value = {
|
||
...current,
|
||
images: [image, ...current.images.filter(item => item.id !== image.id)]
|
||
}
|
||
await detail.refresh()
|
||
}
|
||
},
|
||
{ type: 'info', content: `${name} 的身份参考图已提交生成,完成后会显示结果。` }
|
||
)
|
||
return { accepted, cause: getOperation(project).cause }
|
||
}
|
||
|
||
/**
|
||
* 单角色候选图固定独立生成,不复用旧母版,生成成功后仍需人工确认选角。
|
||
* @param input 已确认的候选图模型与生成参数
|
||
* @returns 任务创建结果及原始错误
|
||
*/
|
||
async function generateCastingCandidate(input: GenerateCastingCandidateInput): Promise<ImageGenerationSubmission> {
|
||
if (!canGenerateCandidate.value || !subject.value || !identity.value) return { accepted: false }
|
||
const id = subject.value.id
|
||
const identityId = identity.value.id
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
const accepted = await runOperation(
|
||
project,
|
||
`提交 ${name} 角色候选图生成`,
|
||
async () => {
|
||
const image = await subjectIdentityApi.generateCastingCandidate(id, input)
|
||
if (!image || image.identityId !== identityId || !['pending', 'generating'].includes(image.status)) {
|
||
throw new Error(image?.error || '选角候选任务未成功创建,请刷新后重试。')
|
||
}
|
||
if (projectId.value === project)
|
||
await Promise.all([
|
||
castingQuery.refresh(),
|
||
catalog.refresh(),
|
||
...(selectedId.value === id ? [detail.refresh()] : [])
|
||
])
|
||
},
|
||
{ type: 'info', content: `${name} 的角色候选图已提交生成,完成后会显示结果。` }
|
||
)
|
||
return { accepted, cause: getOperation(project).cause }
|
||
}
|
||
|
||
/**
|
||
* 候选切换母版只允许成功的 primary 图,不能将辅助视角直接升级为母版。
|
||
* @param imageId 要切换的正式身份图片编号
|
||
*/
|
||
async function setAnchor(imageId: string) {
|
||
const image = images.value.find(item => item.id === imageId)
|
||
if (detailBlocked.value || !subject.value || !image || !canBeAnchor(image) || image.isAnchor) return
|
||
const id = subject.value.id
|
||
const identityId = image.identityId
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
await runImageOperation(
|
||
'confirm',
|
||
imageId,
|
||
`切换 ${name} 身份母版`,
|
||
async () => {
|
||
const result = await subjectIdentityApi.setAnchor(id, imageId)
|
||
if (
|
||
!result ||
|
||
result.id !== imageId ||
|
||
result.identityId !== identityId ||
|
||
!result.isAnchor ||
|
||
!canBeAnchor(result)
|
||
)
|
||
throw new Error('身份母版切换结果未确认,请刷新后核对。')
|
||
await refreshImageConfirmation(project, id)
|
||
},
|
||
{ type: 'success', content: `${name} 的身份母版已切换。` }
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 场景或道具人工确认母版时原子完成母版切换与身份锁定。
|
||
* @param imageId 要确认的正式身份图片编号
|
||
* @param onConfirmed 后端确认成功后的回调,不等待关联数据刷新
|
||
*/
|
||
async function confirmStableAnchor(imageId: string, onConfirmed?: () => void) {
|
||
const image = images.value.find(item => item.id === imageId)
|
||
if (
|
||
detailBlocked.value ||
|
||
(subject.value?.module !== 'scene' && subject.value?.module !== 'prop') ||
|
||
!identity.value ||
|
||
!image ||
|
||
!canBeAnchor(image)
|
||
)
|
||
return
|
||
const subjectId = subject.value.id
|
||
const identityId = identity.value.id
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
await runImageOperation(
|
||
'confirm',
|
||
imageId,
|
||
`确认 ${name} 身份母版`,
|
||
async () => {
|
||
const result = await subjectIdentityApi.confirmStableAnchor(subjectId, imageId)
|
||
if (
|
||
!result ||
|
||
result.identityId !== identityId ||
|
||
result.subjectId !== subjectId ||
|
||
!result.isLocked ||
|
||
result.anchor.id !== imageId ||
|
||
!result.anchor.isAnchor ||
|
||
result.anchor.identityId !== identityId ||
|
||
!canBeAnchor(result.anchor)
|
||
)
|
||
throw new Error('身份母版确认结果未保存,请刷新后核对。')
|
||
onConfirmed?.()
|
||
await refreshImageConfirmation(project, subjectId)
|
||
},
|
||
{ type: 'success', content: `${name} 的身份母版已确认并锁定。` }
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 角色确认选角必须原子切换身份母版并锁定身份。
|
||
* @param imageId 要确认的正式角色图片编号
|
||
* @param onConfirmed 后端确认成功后的回调,不等待关联数据刷新
|
||
*/
|
||
async function confirmCasting(imageId: string, onConfirmed?: () => void) {
|
||
const image = images.value.find(item => item.id === imageId)
|
||
if (
|
||
detailBlocked.value ||
|
||
subject.value?.module !== 'character' ||
|
||
!!castingQuery.error.value ||
|
||
!identity.value ||
|
||
!image ||
|
||
!canBeAnchor(image)
|
||
)
|
||
return
|
||
const subjectId = subject.value.id
|
||
const identityId = identity.value.id
|
||
const project = projectId.value
|
||
const name = subject.value.name
|
||
await runImageOperation(
|
||
'confirm',
|
||
imageId,
|
||
`确认 ${name} 角色选角`,
|
||
async () => {
|
||
const result = await subjectIdentityApi.confirmCasting(subjectId, imageId)
|
||
if (
|
||
!result ||
|
||
result.identityId !== identityId ||
|
||
result.subjectId !== subjectId ||
|
||
!result.isLocked ||
|
||
result.anchor.id !== imageId ||
|
||
!result.anchor.isAnchor ||
|
||
result.anchor.identityId !== identityId ||
|
||
!canBeAnchor(result.anchor)
|
||
)
|
||
throw new Error('角色选角确认结果未保存,请刷新后核对。')
|
||
onConfirmed?.()
|
||
await refreshImageConfirmation(project, subjectId)
|
||
},
|
||
{ type: 'success', content: `${name} 的角色选角已确认,身份已锁定。` }
|
||
)
|
||
}
|
||
|
||
return {
|
||
projectId,
|
||
operation,
|
||
selectedId,
|
||
subject,
|
||
subjects,
|
||
catalog,
|
||
generationModelsQuery,
|
||
castingQuery,
|
||
styleQuery,
|
||
profileQuery,
|
||
detail,
|
||
identity,
|
||
images,
|
||
castingItem,
|
||
session,
|
||
concurrency,
|
||
candidateLimit,
|
||
force,
|
||
editorRevision,
|
||
dirty,
|
||
blocked,
|
||
castingBlocked,
|
||
detailBlocked,
|
||
hasStyle,
|
||
hasCreativeProfile,
|
||
canGenerateText,
|
||
canGenerateImage,
|
||
canGenerateCandidate,
|
||
canRequestImage,
|
||
canRequestCandidate,
|
||
activeIdentityGeneration,
|
||
batchValid,
|
||
candidateLimitValid,
|
||
save,
|
||
generate,
|
||
generateProject,
|
||
generateStableAnchors,
|
||
generateCastingCandidates,
|
||
generateImage,
|
||
generateCastingCandidate,
|
||
setAnchor,
|
||
confirmStableAnchor,
|
||
confirmCasting,
|
||
validateImage,
|
||
pendingImageOperation,
|
||
generateStableAnchorQuality,
|
||
validationResult,
|
||
qualityResult
|
||
}
|
||
}
|