467 lines
21 KiB
Vue
467 lines
21 KiB
Vue
<script setup lang="ts">
|
||
import { useQuery } from '../../../composables/useQuery'
|
||
import { imageAuthorizationApi } from '../../image-authorization/api'
|
||
import { formatMicros } from '../../image-authorization/model'
|
||
import { NSelect } from 'naive-ui'
|
||
import KeyframePreviewChecks from './KeyframePreviewChecks.vue'
|
||
import ModelCapabilities from './ModelCapabilities.vue'
|
||
import ImageGenerationPreview from '../../subject-images/components/ImageGenerationPreview.vue'
|
||
import { useImageGenerationPreview, type ImageGenerationSubmission } from '../../subject-images/generation-preview'
|
||
|
||
import ContextNotice from '../../../components/ui/ContextNotice.vue'
|
||
import RecordDetails from '../../../components/ui/RecordDetails.vue'
|
||
|
||
import AppForm from '../../../components/ui/AppForm.vue'
|
||
import { NButton, NCheckbox, NCollapse, NCollapseItem } from 'naive-ui'
|
||
import { computed, ref, watch } from 'vue'
|
||
import { generationConfigApi } from '../../generation-config/api'
|
||
import type {
|
||
CreativeProfileAspectRatio,
|
||
GenerationModelCatalog,
|
||
GenerationOptions
|
||
} from '../../generation-config/types'
|
||
import GenerationModelControls from '../../generation-config/components/GenerationModelControls.vue'
|
||
import { AppDialog, FieldLabel } from '../../../components/ui'
|
||
import { ApiError, errorMessage } from '../../../lib/http'
|
||
import { productionApi } from '../api'
|
||
import { directionLabel } from '../../storyboard/model'
|
||
import type { ShotDirection, ShotVisualState } from '../../storyboard/types'
|
||
import type { GenerateKeyframeInput } from '../types'
|
||
|
||
/** 单镜头首帧表单;所有新图先生成候选,通过质量检查后再人工晋升。 */
|
||
const props = defineProps<{
|
||
shotId: string
|
||
shotTitle: string
|
||
projectId: string
|
||
disabled: boolean
|
||
/** 原镜头条件仍有效时允许免费核对追加许可,不放开普通未知重发。 */
|
||
additionalAllowed?: boolean
|
||
replacePrimary?: boolean
|
||
/** 当前镜头的正式摄影设计。 */
|
||
direction?: ShotDirection | null
|
||
/** 当前镜头的正式画面即时状态。 */
|
||
visualState?: ShotVisualState | null
|
||
/** 提交前已有候选,用于区分下载恢复与历史候选。 */
|
||
previousCandidateIds?: readonly string[]
|
||
/** 免费查看经当前预览核实的已有完整候选,不产生POST。 */
|
||
reuseExisting?: (fingerprint: string) => Promise<boolean>
|
||
/** 仅发送已经确认的当前快照。 */
|
||
submitGeneration: (input: GenerateKeyframeInput) => Promise<ImageGenerationSubmission>
|
||
}>()
|
||
/** 当前候选生成窗口。 */
|
||
const open = defineModel<boolean>('open', { default: false })
|
||
/** 生成参数表单,用于复用字段校验。 */
|
||
const formRef = ref<InstanceType<typeof AppForm> | null>(null)
|
||
/** 摄影枚举复用分镜页面的中文展示。 */
|
||
const directionSummary = computed(() =>
|
||
props.direction
|
||
? Object.entries(props.direction)
|
||
.filter(
|
||
([field, value]) =>
|
||
[
|
||
'shotSize',
|
||
'cameraAngle',
|
||
'cameraMovement',
|
||
'composition',
|
||
'cameraPosition',
|
||
'lensIntent',
|
||
'depthOfField',
|
||
'visualIntent'
|
||
].includes(field) && value
|
||
)
|
||
.map(([field, value]) => directionLabel(value, field))
|
||
.join(' · ')
|
||
: ''
|
||
)
|
||
/** 本次目录与模型参数仅用于首帧。 */
|
||
const generationCatalog = ref<GenerationModelCatalog | null>(null)
|
||
/** 模型读取错误。 */
|
||
const generationCatalogError = ref('')
|
||
/** 实际项目及镜头配置解析后的模型。 */
|
||
const imageProvider = ref('')
|
||
/** 当前用户主动选择的许可,重新打开或切换范围后不保留默认选择。 */
|
||
const additionalId = ref<string | null>(null)
|
||
/** 人工追加只属于OpenAI,不改变其它供应方的未知请求保护。 */
|
||
const canInspectAdditional = computed(
|
||
() => (!!props.additionalAllowed || !props.disabled) && imageProvider.value === 'openai-image'
|
||
)
|
||
/** 仅需要核对原失败请求时读取已有许可,不自动创建。 */
|
||
const additionalKey = computed(() =>
|
||
open.value && canInspectAdditional.value ? JSON.stringify([props.projectId, props.shotId]) : ''
|
||
)
|
||
/** 当前原项目的有限许可和原unknown历史。 */
|
||
const additionalQuery = useQuery(additionalKey, async (value, signal) => {
|
||
if (!value) return null
|
||
const [projectId] = JSON.parse(value) as [string, string]
|
||
const result = await imageAuthorizationApi.list(projectId, signal)
|
||
if (!result || !Array.isArray(result.requests) || !Array.isArray(result.authorizations))
|
||
throw new Error('原请求与授权记录读取不完整,请重新核对。')
|
||
return result
|
||
})
|
||
/** 只列原镜头已有且未撤销、尚有次数的许可,不自动选中。 */
|
||
const additionalOptions = computed(() =>
|
||
(additionalQuery.data.value?.additionalAuthorizations ?? [])
|
||
.filter(
|
||
permit =>
|
||
permit.status === 'approved' &&
|
||
permit.remainingRequests > 0 &&
|
||
(permit.input.mode === 'bounded'
|
||
? Date.parse(permit.input.expiresAt) > Date.now() &&
|
||
permit.targets?.some(
|
||
target => target.shotId === props.shotId && target.usedRequests < target.maxRequests
|
||
)
|
||
: additionalQuery.data.value?.requests.some(
|
||
row =>
|
||
row.id ===
|
||
(permit.input.mode === 'additional' ? permit.input.parentRequestId : undefined) &&
|
||
row.input.projectId === props.projectId &&
|
||
row.input.targetId === props.shotId &&
|
||
row.input.scope === 'keyframe'
|
||
))
|
||
)
|
||
.map(permit => ({
|
||
label: `${permit.input.approvedBy} · 已用 ${permit.usedRequests} / 剩余 ${permit.remainingRequests} 次`,
|
||
value: permit.id
|
||
}))
|
||
)
|
||
/** 已选许可及其原unknown,不改写原范围或金额。 */
|
||
const additionalPermit = computed(() =>
|
||
additionalQuery.data.value?.additionalAuthorizations?.find(permit => permit.id === additionalId.value)
|
||
)
|
||
/** 原未知费用与预留保持显示。 */
|
||
const additionalParent = computed(() =>
|
||
additionalQuery.data.value?.requests.find(
|
||
row =>
|
||
row.id ===
|
||
(additionalPermit.value?.input.mode === 'additional'
|
||
? additionalPermit.value.input.parentRequestId
|
||
: undefined)
|
||
)
|
||
)
|
||
/** 有限跨镜许可的本镜计数及原预算保持独立。 */
|
||
const boundedTarget = computed(() =>
|
||
additionalPermit.value?.input.mode === 'bounded'
|
||
? additionalPermit.value.targets?.find(row => row.shotId === props.shotId)
|
||
: null
|
||
)
|
||
/** 原standing预算,不把许可次数显示成另一笔预算。 */
|
||
const boundedBudget = computed(() =>
|
||
additionalPermit.value?.input.mode === 'bounded'
|
||
? additionalQuery.data.value?.authorizations.find(
|
||
row =>
|
||
row.id ===
|
||
(additionalPermit.value?.input.mode === 'bounded'
|
||
? additionalPermit.value.input.parentAuthorizationId
|
||
: undefined)
|
||
)
|
||
: null
|
||
)
|
||
/** 仅有真实阻断或可选许可时展示人工准入,普通有效授权不常驻未知费用说明。 */
|
||
const showAdditionalOptions = computed(
|
||
() =>
|
||
canInspectAdditional.value &&
|
||
(!!additionalId.value ||
|
||
!!additionalOptions.value.length ||
|
||
additionalQuery.loading.value ||
|
||
!!additionalQuery.error.value ||
|
||
additionalQuery.data.value?.billingBlocked)
|
||
)
|
||
watch(
|
||
[() => props.projectId, () => props.shotId, open],
|
||
() => {
|
||
additionalId.value = null
|
||
},
|
||
{ flush: 'sync' }
|
||
)
|
||
/** 本次明确参数。 */
|
||
const imageGenerationOptions = ref<GenerationOptions>({})
|
||
/** 项目画幅。 */
|
||
const projectAspectRatio = ref<CreativeProfileAspectRatio>('9:16')
|
||
/** 预览和生成复用当前请求字段,生成时再附加版本及候选标记。 */
|
||
const generationInput = computed(() => ({
|
||
provider: imageProvider.value || undefined,
|
||
generationOptions: imageGenerationOptions.value,
|
||
...(additionalId.value ? { additionalAuthorizationId: additionalId.value } : {})
|
||
}))
|
||
/** 首帧与主体图片使用同一套预览确认与未知费用保护。 */
|
||
const {
|
||
preview,
|
||
acknowledged,
|
||
requiresCostConfirmation,
|
||
chargeNotice,
|
||
reusableResult,
|
||
invalidate: invalidatePreview,
|
||
loading,
|
||
submitting,
|
||
error,
|
||
staleNotice,
|
||
recoveryNotice,
|
||
canSubmit,
|
||
loadPreview,
|
||
submit: submitPreview
|
||
} = useImageGenerationPreview({
|
||
open,
|
||
chargeContext: () => ({
|
||
projectId: props.projectId,
|
||
scope: 'keyframe',
|
||
targetId: props.shotId,
|
||
exactPreview: true
|
||
}),
|
||
additionalAuthorizationId: () => additionalId.value ?? undefined,
|
||
recoveryTarget: () =>
|
||
props.shotId
|
||
? additionalId.value && additionalPermit.value
|
||
? `keyframe:${props.projectId}:${props.shotId}:additional:${additionalId.value}:used:${additionalPermit.value.usedRequests}`
|
||
: 'keyframe:' + props.shotId
|
||
: '',
|
||
previousCandidateIds: () => props.previousCandidateIds,
|
||
target: () => JSON.stringify([props.projectId, props.shotId, props.direction, props.visualState]),
|
||
input: () => generationInput.value,
|
||
disabled: () =>
|
||
(props.disabled && !canInspectAdditional.value) ||
|
||
!imageProvider.value ||
|
||
(!!additionalId.value &&
|
||
(imageProvider.value !== 'openai-image' || additionalQuery.loading.value || !!additionalQuery.error.value)),
|
||
load: async (input, signal) => {
|
||
const shotId = props.shotId
|
||
const result = await productionApi.previewKeyframe(
|
||
shotId,
|
||
{ provider: input.provider, generationOptions: input.generationOptions },
|
||
signal
|
||
)
|
||
if (result?.shotId !== shotId) throw new Error('预览结果与当前镜头不匹配。')
|
||
return result
|
||
},
|
||
reuseResult: props.reuseExisting ? result => props.reuseExisting!(result.previewFingerprint) : undefined,
|
||
generate: async input => {
|
||
const outcome = await props.submitGeneration({ ...input, setPrimary: false })
|
||
if (!outcome.accepted && outcome.cause instanceof ApiError && outcome.cause.status === 409) {
|
||
await additionalQuery.refresh()
|
||
if (
|
||
outcome.cause.code === 'SCENE_CONTINUITY_REVIEW_STALE' ||
|
||
outcome.cause.message.startsWith('SCENE_CONTINUITY_REVIEW_STALE')
|
||
)
|
||
await Promise.allSettled([productionApi.keyframeSpec(props.shotId, undefined, props.projectId)])
|
||
}
|
||
return outcome
|
||
}
|
||
})
|
||
|
||
/** 正式预览返回连续性阻断时,键盘和底栏提交均不可继续。 */
|
||
async function submit() {
|
||
if (preview.value?.specConsistencySummary?.blocking) return
|
||
await submitPreview()
|
||
}
|
||
|
||
/** 切换镜头或重新打开时只读取模型配置,忽略已关闭弹窗的结果。 */
|
||
watch(
|
||
[() => props.shotId, () => props.projectId, open],
|
||
async ([, projectId, isOpen], _, onCleanup) => {
|
||
if (!isOpen) return
|
||
const controller = new AbortController()
|
||
onCleanup(() => controller.abort())
|
||
generationCatalog.value = null
|
||
generationCatalogError.value = ''
|
||
imageProvider.value = ''
|
||
try {
|
||
const [catalog, profile, resolved] = await Promise.all([
|
||
generationConfigApi.catalog(controller.signal),
|
||
generationConfigApi.profile(projectId, controller.signal),
|
||
generationConfigApi.resolve(
|
||
projectId,
|
||
'image',
|
||
{
|
||
target: { targetType: 'shot', targetId: props.shotId }
|
||
},
|
||
controller.signal
|
||
)
|
||
])
|
||
if (controller.signal.aborted) return
|
||
if (!resolved.provider || !resolved.model || !resolved.options)
|
||
throw new Error('当前镜头模型配置不完整,请重新读取。')
|
||
generationCatalog.value = catalog
|
||
projectAspectRatio.value = profile?.aspectRatio ?? '9:16'
|
||
imageProvider.value = resolved.provider
|
||
const selected = catalog.images.find(item => item.provider === imageProvider.value)
|
||
imageGenerationOptions.value = { ...selected?.defaults, ...resolved.options }
|
||
} catch (cause) {
|
||
if (!controller.signal.aborted) generationCatalogError.value = errorMessage(cause)
|
||
}
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
</script>
|
||
|
||
<template>
|
||
<AppDialog
|
||
v-model:open="open"
|
||
:title="`${replacePrimary ? '生成替代候选' : '生成首帧候选'} · ${shotTitle}`"
|
||
description="新图通过检查后可设为主首帧。"
|
||
:busy="submitting"
|
||
description-as-info
|
||
>
|
||
<AppForm
|
||
ref="formRef"
|
||
:model="imageGenerationOptions"
|
||
:disabled="(disabled && !canInspectAdditional) || submitting"
|
||
:reset-key="`${open}:${shotId}`"
|
||
validate-on-change
|
||
@submit="submit"
|
||
>
|
||
<ContextNotice v-if="replacePrimary" type="info" :show-icon="false" class="mt-5 text-xs">
|
||
参考图已变化。新候选通过后可设为主首帧,旧图会保留。
|
||
</ContextNotice>
|
||
<section v-if="generationCatalog" class="dialog-section mt-5" aria-label="本次首帧生成模型设置">
|
||
<p class="text-xs font-medium">
|
||
<FieldLabel label="本次生成设置" description="仅用于本次首帧生成。" />
|
||
</p>
|
||
<GenerationModelControls
|
||
class="mt-4"
|
||
type="image"
|
||
:catalog="generationCatalog"
|
||
:provider="imageProvider"
|
||
:options="imageGenerationOptions"
|
||
:aspect-ratio="projectAspectRatio"
|
||
:disabled="submitting"
|
||
@update:provider="imageProvider = $event"
|
||
@update:options="imageGenerationOptions = $event"
|
||
/>
|
||
</section>
|
||
<ContextNotice v-else-if="generationCatalogError" type="warning" :show-icon="false" class="mt-5 text-xs">
|
||
生成模型目录读取失败:{{ generationCatalogError }}
|
||
</ContextNotice>
|
||
<!-- 以下是用户主动选择的原精确计划追加许可,普通unknown不放行。 -->
|
||
<section v-if="showAdditionalOptions" class="workspace-panel workspace-section-stack mt-5">
|
||
<p v-if="additionalQuery.data.value?.billingBlocked" class="text-sm">
|
||
原请求在途或费用待核对。有限许可不会核清旧费用。
|
||
</p>
|
||
<FieldLabel
|
||
label="人工有限许可"
|
||
description="只作用于已批准的镜头和精确计划;每次由你主动操作,不自动消耗剩余次数。"
|
||
icon="info"
|
||
/>
|
||
<NSelect
|
||
v-model:value="additionalId"
|
||
clearable
|
||
:options="additionalOptions"
|
||
:disabled="submitting || additionalQuery.loading.value"
|
||
placeholder="选择人工有限许可(可选)"
|
||
aria-label="本次人工追加许可"
|
||
/><NButton
|
||
:loading="additionalQuery.loading.value"
|
||
:disabled="submitting"
|
||
@click="additionalQuery.refresh"
|
||
>刷新追加许可与原请求</NButton
|
||
>
|
||
<p v-if="additionalQuery.error.value" role="alert">{{ additionalQuery.error.value }}</p>
|
||
<p v-if="additionalParent" class="text-sm">
|
||
原型号 {{ additionalParent.input.model }} · 原预留
|
||
{{ formatMicros(additionalParent.input.reservedMicros) }} · 原账单
|
||
{{ formatMicros(additionalParent.output?.actualBilledMicros) }}
|
||
</p>
|
||
<p v-if="boundedTarget" class="text-sm">
|
||
本镜已用 {{ boundedTarget.usedRequests }}/{{ boundedTarget.maxRequests }} 次 · 原预算剩余
|
||
{{ formatMicros(boundedBudget?.remainingMicros) }}。旧未知费用未被核清。
|
||
</p>
|
||
<RecordDetails
|
||
v-if="additionalPermit"
|
||
title="许可记录"
|
||
:records="[
|
||
{
|
||
fields: [
|
||
{
|
||
label: '适用方式',
|
||
value:
|
||
additionalPermit.input.mode === 'bounded'
|
||
? '限定镜头与计划'
|
||
: '同一计划人工追加'
|
||
},
|
||
{ label: '批准人', value: additionalPermit.input.approvedBy },
|
||
{ label: '批准说明', value: additionalPermit.input.approvalNote },
|
||
{ label: '批准次数', value: additionalPermit.input.maxRequests },
|
||
{ label: '已用次数', value: additionalPermit.usedRequests },
|
||
{ label: '剩余次数', value: additionalPermit.remainingRequests }
|
||
]
|
||
}
|
||
]"
|
||
/>
|
||
</section>
|
||
<!-- 以下是正式摄影设计与画面状态 -->
|
||
<ModelCapabilities
|
||
v-if="open && imageProvider"
|
||
:shot-id="shotId"
|
||
:provider="imageProvider"
|
||
@checked="!$event && invalidatePreview()"
|
||
/>
|
||
<NCollapse v-if="direction || visualState" class="mt-5">
|
||
<NCollapseItem v-if="direction" name="direction" title="当前镜头设计">
|
||
<p class="text-xs leading-6">{{ directionSummary }}</p>
|
||
</NCollapseItem>
|
||
<NCollapseItem v-if="visualState" name="visual-state" title="当前画面状态">
|
||
<p class="text-xs leading-6">
|
||
{{
|
||
[
|
||
visualState.timeOfDay,
|
||
visualState.weather,
|
||
visualState.atmosphere,
|
||
visualState.environmentTransientState,
|
||
visualState.continuityNote
|
||
]
|
||
.filter(Boolean)
|
||
.join(' · ')
|
||
}}
|
||
</p>
|
||
<p v-for="subject in visualState.subjects" :key="subject.subjectId" class="mt-2 text-xs leading-6">
|
||
{{
|
||
[
|
||
subject.subjectRef,
|
||
subject.formName,
|
||
subject.position,
|
||
subject.pose,
|
||
subject.expression,
|
||
subject.gaze,
|
||
subject.transientState
|
||
]
|
||
.filter(Boolean)
|
||
.join(' · ')
|
||
}}
|
||
</p>
|
||
</NCollapseItem>
|
||
</NCollapse>
|
||
<!-- 以下是最终模型输入和正式参考资产 -->
|
||
<ImageGenerationPreview
|
||
class="mt-5"
|
||
:preview="preview"
|
||
:loading="loading"
|
||
:disabled="!imageProvider || submitting"
|
||
:error="error"
|
||
:stale-notice="staleNotice || recoveryNotice"
|
||
@load="loadPreview"
|
||
/>
|
||
<!-- 以下是正式画幅、尺寸及连续性诊断 -->
|
||
<KeyframePreviewChecks v-if="preview" :preview="preview" class="mt-3" />
|
||
<ContextNotice v-if="chargeNotice" emphasized :show-icon="false" role="status">{{
|
||
chargeNotice
|
||
}}</ContextNotice>
|
||
<NCheckbox
|
||
v-if="requiresCostConfirmation"
|
||
id="confirm-keyframe-cost"
|
||
v-model:checked="acknowledged"
|
||
:disabled="!preview || preview.specConsistencySummary?.blocking || submitting || !!recoveryNotice"
|
||
class="mt-5 flex items-start gap-3 text-sm leading-6"
|
||
>我已核对实际输入,并确认本次图片生成可能产生费用。
|
||
</NCheckbox>
|
||
</AppForm>
|
||
<template #footer>
|
||
<NButton :disabled="submitting" @click="open = false">取消</NButton>
|
||
<NButton
|
||
:disabled="!canSubmit || preview?.specConsistencySummary?.blocking"
|
||
:loading="submitting"
|
||
@click="formRef?.validate(submit)"
|
||
type="primary"
|
||
>{{ reusableResult ? '查看已有同计划候选' : '确认生成首帧' }}</NButton
|
||
>
|
||
</template>
|
||
</AppDialog>
|
||
</template>
|