371 lines
15 KiB
Vue
371 lines
15 KiB
Vue
<script setup lang="ts">
|
||
import ContextNotice from '../../../components/ui/ContextNotice.vue'
|
||
import WorkspaceSectionHeading from '../../../components/ui/WorkspaceSectionHeading.vue'
|
||
|
||
import { computed, onScopeDispose, ref, watch } from 'vue'
|
||
import { NButton, NFormItem, NSelect, NSpin } from 'naive-ui'
|
||
import { ApiError, errorMessage } from '../../../lib/http'
|
||
import { showActionFeedback, useActionMessage } from '../../../components/ui/useActionMessage'
|
||
import { generationConfigApi } from '../api'
|
||
import type {
|
||
CreativeProfileAspectRatio,
|
||
CreativeProfileInput,
|
||
GenerationModelCatalog,
|
||
GenerationModelItem,
|
||
GenerationOptions
|
||
} from '../types'
|
||
import { creativeProfileDraft, creativeProfileIssues } from '../profile-draft'
|
||
import { useProjectGenerationRevision } from '../context'
|
||
import GenerationOptionFields from './GenerationOptionFields.vue'
|
||
|
||
/** 项目配置表单参数。 */
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
/** 当前项目 ID。 */
|
||
projectId: string
|
||
/** 弹窗由公共 footer 提供保存操作,隐藏表单自身操作区。 */
|
||
hideActions?: boolean
|
||
/** 有项目操作时禁止编辑和保存配置。 */
|
||
disabled?: boolean
|
||
}>(),
|
||
{ hideActions: false, disabled: false }
|
||
)
|
||
/** 保存后的实际配置与保存状态通知。 */
|
||
const emit = defineEmits<{ saved: [profile: CreativeProfileInput]; busy: [saving: boolean] }>()
|
||
/** 成功保存后通知当前项目生成表单更新默认值。 */
|
||
const revision = useProjectGenerationRevision()
|
||
/** 保存结果复用全局消息层,读取失败仍保留可核对的正文状态。 */
|
||
const feedback = useActionMessage()
|
||
|
||
/** 配置和模型目录读取状态。 */
|
||
const loading = ref(false)
|
||
/** 配置保存状态。 */
|
||
const saving = ref(false)
|
||
/** 配置读取失败的可恢复状态,不用于保存反馈。 */
|
||
const error = ref('')
|
||
/** 后端支持的图片与视频模型目录。 */
|
||
const catalog = ref<GenerationModelCatalog | null>(null)
|
||
/** 当前项目的独立编辑草稿。 */
|
||
const profile = ref<CreativeProfileInput | null>(null)
|
||
/** 当前读取请求控制器。 */
|
||
let controller: AbortController | null = null
|
||
/** 组件销毁后忽略迟到保存响应,不更新新项目配置修订号。 */
|
||
let disposed = false
|
||
|
||
/** 项目画布支持的宽高比选项。 */
|
||
const aspectRatios = ['9:16', '16:9', '1:1', '4:3', '3:4'].map(value => ({ label: value, value }))
|
||
/** 当前可用的图片模型。 */
|
||
const enabledImages = computed(() => catalog.value?.images?.filter(item => item.enabled && item.model) ?? [])
|
||
/** 当前可用的视频模型。 */
|
||
const enabledVideos = computed(() => catalog.value?.videos?.filter(item => item.enabled && item.model) ?? [])
|
||
/** 当前图片模型的组合选项值。 */
|
||
const imageSelection = computed(() =>
|
||
profile.value ? selectionValue(profile.value.imageProvider, profile.value.imageModel) : null
|
||
)
|
||
/** 当前视频模型的组合选项值。 */
|
||
const videoSelection = computed(() =>
|
||
profile.value ? selectionValue(profile.value.videoProvider, profile.value.videoModel) : null
|
||
)
|
||
/** 图片模型对应的参数 schema。 */
|
||
const imageModel = computed(() =>
|
||
enabledImages.value.find(item =>
|
||
profile.value ? item.provider === profile.value.imageProvider && item.model === profile.value.imageModel : false
|
||
)
|
||
)
|
||
/** 视频模型对应的参数 schema。 */
|
||
const videoModel = computed(() =>
|
||
enabledVideos.value.find(item =>
|
||
profile.value ? item.provider === profile.value.videoProvider && item.model === profile.value.videoModel : false
|
||
)
|
||
)
|
||
|
||
/**
|
||
* 组合供应方与模型,避免同名模型混淆。
|
||
* @param provider 模型供应方
|
||
* @param model 模型名称
|
||
* @returns 下拉选项唯一值
|
||
*/
|
||
function selectionValue(provider: string, model: string) {
|
||
return `${provider}\u0000${model}`
|
||
}
|
||
|
||
/**
|
||
* 将实际模型目录转成下拉选项。
|
||
* @param items 当前媒介模型列表
|
||
* @returns 模型下拉选项
|
||
*/
|
||
function modelOptions(items: GenerationModelItem[]) {
|
||
return items.map(item => ({
|
||
label: `${item.providerLabel} · ${item.modelLabel}`,
|
||
value: selectionValue(item.provider, item.model || '')
|
||
}))
|
||
}
|
||
|
||
/**
|
||
* 依据目标模型 schema 创建合法初始参数。
|
||
* @param item 当前模型
|
||
* @returns 模型初始参数
|
||
*/
|
||
function defaultOptions(item: GenerationModelItem): GenerationOptions {
|
||
return Object.fromEntries(
|
||
item.generationOptions.flatMap(schema => {
|
||
if (schema.providerDefault !== undefined) return [[schema.key, schema.providerDefault]]
|
||
if (!schema.required) return []
|
||
if (schema.type === 'boolean') return [[schema.key, false]]
|
||
if (schema.type === 'select' && schema.options?.length) {
|
||
const first = schema.options[0]
|
||
return first ? [[schema.key, first.value]] : []
|
||
}
|
||
if ((schema.type === 'integer' || schema.type === 'number') && schema.min !== undefined)
|
||
return [[schema.key, schema.min]]
|
||
if (schema.type === 'size' && schema.options?.length) {
|
||
const first = schema.options[0]
|
||
return first ? [[schema.key, first.value]] : []
|
||
}
|
||
return []
|
||
})
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 切换模型时清空上一模型参数。
|
||
* @param type 图片或视频媒介
|
||
* @param value 下拉选项唯一值
|
||
*/
|
||
function selectModel(type: 'image' | 'video', value: string) {
|
||
if (!profile.value) return
|
||
const items = type === 'image' ? enabledImages.value : enabledVideos.value
|
||
const item = items.find(row => selectionValue(row.provider, row.model || '') === value)
|
||
if (!item?.model) return
|
||
if (type === 'image') {
|
||
profile.value.imageProvider = item.provider
|
||
profile.value.imageModel = item.model
|
||
profile.value.imageOptions = defaultOptions(item)
|
||
} else {
|
||
profile.value.videoProvider = item.provider
|
||
profile.value.videoModel = item.model
|
||
profile.value.videoOptions = defaultOptions(item)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 未配置项目使用后端可用默认模型初始化草稿。
|
||
* @param nextCatalog 当前模型目录
|
||
* @returns 初始项目配置,无完整模型时返回 null
|
||
*/
|
||
function createInitial(nextCatalog: GenerationModelCatalog): CreativeProfileInput | null {
|
||
const image =
|
||
nextCatalog.images.find(item => item.enabled && item.isDefault && item.model) ??
|
||
nextCatalog.images.find(item => item.enabled && item.model)
|
||
const video =
|
||
nextCatalog.videos.find(item => item.enabled && item.isDefault && item.model) ??
|
||
nextCatalog.videos.find(item => item.enabled && item.model)
|
||
if (!image?.model || !video?.model) return null
|
||
return {
|
||
aspectRatio: '9:16',
|
||
imageProvider: image.provider,
|
||
imageModel: image.model,
|
||
imageOptions: defaultOptions(image),
|
||
videoProvider: video.provider,
|
||
videoModel: video.model,
|
||
videoOptions: defaultOptions(video)
|
||
}
|
||
}
|
||
|
||
/** 读取实际目录与项目配置,取消旧目标请求并忽略迟到响应。 */
|
||
async function load() {
|
||
controller?.abort()
|
||
controller = new AbortController()
|
||
const signal = controller.signal
|
||
const projectId = props.projectId
|
||
profile.value = null
|
||
catalog.value = null
|
||
loading.value = true
|
||
error.value = ''
|
||
try {
|
||
const [nextCatalog, saved] = await Promise.all([
|
||
generationConfigApi.catalog(signal),
|
||
generationConfigApi.profile(projectId, signal)
|
||
])
|
||
if (signal.aborted || props.projectId !== projectId) return
|
||
if (saved && saved.projectId !== projectId) throw new Error('项目配置返回了不匹配的项目。')
|
||
catalog.value = nextCatalog
|
||
profile.value = saved ? creativeProfileDraft(saved) : createInitial(nextCatalog)
|
||
} catch (cause) {
|
||
if (!signal.aborted && props.projectId === projectId) error.value = errorMessage(cause)
|
||
} finally {
|
||
if (!signal.aborted && props.projectId === projectId) loading.value = false
|
||
}
|
||
}
|
||
|
||
/** 保存独立配置快照,不因路由变化把结果写入其它项目。 */
|
||
async function save() {
|
||
if (!canSave.value || !profile.value) return
|
||
const projectId = props.projectId
|
||
const input = creativeProfileDraft(profile.value)
|
||
saving.value = true
|
||
error.value = ''
|
||
try {
|
||
const saved = await generationConfigApi.saveProfile(projectId, input)
|
||
if (disposed || props.projectId !== projectId) return
|
||
if (saved.projectId !== projectId) throw new Error('保存结果与当前项目不匹配,请重新读取配置。')
|
||
profile.value = creativeProfileDraft(saved)
|
||
showActionFeedback(feedback, '项目生成配置已保存,后续制作将使用此配置。', 'success')
|
||
revision.value++
|
||
emit('saved', profile.value)
|
||
} catch (cause) {
|
||
if (!disposed && props.projectId === projectId)
|
||
showActionFeedback(
|
||
feedback,
|
||
errorMessage(cause),
|
||
cause instanceof ApiError && cause.status === 409 ? 'warning' : 'error'
|
||
)
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
/** 按后端实际 schema 展示不兼容参数,不静默替换用户已保存的值。 */
|
||
const validationIssues = computed(() =>
|
||
profile.value && catalog.value ? creativeProfileIssues(profile.value, catalog.value) : []
|
||
)
|
||
/** 有实际项目草稿、完整可用模型且无进行中的项目操作才允许保存。 */
|
||
const canSave = computed(
|
||
() =>
|
||
!props.disabled &&
|
||
!loading.value &&
|
||
!saving.value &&
|
||
!!profile.value &&
|
||
!!enabledImages.value.length &&
|
||
!!enabledVideos.value.length &&
|
||
!validationIssues.value.length
|
||
)
|
||
/** 向弹窗 footer 暴露同一个保存入口,避免复制表单与请求逻辑。 */
|
||
defineExpose({ save, canSave })
|
||
|
||
/** 保存状态同步给弹窗,保存完成前禁止关闭。 */
|
||
watch(saving, value => emit('busy', value), { flush: 'sync' })
|
||
/** 项目变化时重新读取配置。 */
|
||
watch(() => props.projectId, load, { immediate: true })
|
||
onScopeDispose(() => {
|
||
disposed = true
|
||
controller?.abort()
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<!-- 以下是平铺的项目画布与模型配置模块,使用留白分组而不叠加背景面板 -->
|
||
<section class="production-tool-section creative-profile-settings">
|
||
<h3 v-if="!hideActions" class="text-sm font-semibold">项目画布与生成模型</h3>
|
||
<NSpin :show="loading">
|
||
<ContextNotice v-if="disabled" type="info" :show-icon="false" class="mb-3"
|
||
>项目正在执行制作任务,完成后可以修改配置。</ContextNotice
|
||
>
|
||
<ContextNotice v-if="error" type="error" :show-icon="false" class="mb-3">{{ error }}</ContextNotice>
|
||
<ContextNotice v-if="validationIssues.length" type="warning" :show-icon="false" class="mb-3">
|
||
<p v-for="issue in validationIssues" :key="issue">{{ issue }}</p>
|
||
</ContextNotice>
|
||
<ContextNotice
|
||
v-if="!loading && (!enabledImages.length || !enabledVideos.length)"
|
||
type="warning"
|
||
:show-icon="false"
|
||
>
|
||
请先配置可用的图片和视频模型。
|
||
</ContextNotice>
|
||
<div v-if="profile" class="creative-profile-grid">
|
||
<!-- 以下是独占一行的画布设置,与下方两个模型分组共用边界 -->
|
||
<section class="creative-profile-canvas space-y-4">
|
||
<WorkspaceSectionHeading title="画布设置" description="用于后续图片和视频制作。" />
|
||
<NFormItem label="作品宽高比">
|
||
<NSelect
|
||
:value="profile.aspectRatio"
|
||
:options="aspectRatios"
|
||
:disabled="saving || disabled"
|
||
@update:value="profile.aspectRatio = $event as CreativeProfileAspectRatio"
|
||
/>
|
||
</NFormItem>
|
||
</section>
|
||
<!-- 以下是项目图片生成配置模块 -->
|
||
<section class="model-section min-w-0">
|
||
<WorkspaceSectionHeading class="mb-4" title="图片配置" />
|
||
<NFormItem label="图片模型">
|
||
<NSelect
|
||
:value="imageSelection"
|
||
:options="modelOptions(enabledImages)"
|
||
:disabled="saving || disabled"
|
||
@update:value="selectModel('image', $event)"
|
||
/>
|
||
</NFormItem>
|
||
<GenerationOptionFields
|
||
v-model="profile.imageOptions"
|
||
:schemas="imageModel?.generationOptions || []"
|
||
:disabled="saving || disabled"
|
||
/>
|
||
</section>
|
||
<!-- 以下是项目视频生成配置模块 -->
|
||
<section class="model-section min-w-0">
|
||
<WorkspaceSectionHeading class="mb-4" title="视频配置" />
|
||
<NFormItem label="视频模型">
|
||
<NSelect
|
||
:value="videoSelection"
|
||
:options="modelOptions(enabledVideos)"
|
||
:disabled="saving || disabled"
|
||
@update:value="selectModel('video', $event)"
|
||
/>
|
||
</NFormItem>
|
||
<GenerationOptionFields
|
||
v-model="profile.videoOptions"
|
||
:schemas="videoModel?.generationOptions || []"
|
||
:disabled="saving || disabled"
|
||
/>
|
||
</section>
|
||
</div>
|
||
<div v-if="!hideActions" class="page-form-actions creative-profile-actions">
|
||
<NButton type="primary" :loading="saving" :disabled="!canSave" @click="save">保存项目生成配置</NButton>
|
||
<span class="text-xs text-muted">切换模型时会按新模型重置生成参数。</span>
|
||
</div>
|
||
</NSpin>
|
||
</section>
|
||
</template>
|
||
|
||
<style scoped>
|
||
@reference "../../../styles/styles.css";
|
||
.creative-profile-grid {
|
||
@apply grid grid-cols-[repeat(2,_minmax(0,_1fr))] items-start gap-x-10 gap-y-6;
|
||
}
|
||
/* 画布设置铺满一行,图片与视频在宽屏并排、窄屏依次显示。 */
|
||
.creative-profile-canvas {
|
||
grid-column: 1 / -1;
|
||
}
|
||
.model-section > :deep(.n-form-item) {
|
||
@apply mb-6;
|
||
}
|
||
/* 模块首字段与动态参数字段使用相同的标签和控件行高。 */
|
||
.creative-profile-grid :deep(.n-form-item--top-labelled) {
|
||
grid-template-rows: minmax(var(--n-label-height), auto) auto auto;
|
||
}
|
||
.creative-profile-grid :deep(.n-form-item-label) {
|
||
min-height: 30px;
|
||
padding: 0 0 8px;
|
||
}
|
||
.creative-profile-grid :deep(.n-form-item-blank) {
|
||
min-height: 34px;
|
||
align-items: center;
|
||
}
|
||
.creative-profile-settings {
|
||
/* 与质量规则页的首模块保持相同的顶部留白。 */
|
||
@apply m-0 pt-5;
|
||
}
|
||
.creative-profile-settings > h3 {
|
||
@apply mb-4;
|
||
}
|
||
.creative-profile-actions {
|
||
@apply mt-4;
|
||
}
|
||
@media (max-width: 900px) {
|
||
.creative-profile-grid {
|
||
@apply grid-cols-[minmax(0,_1fr)];
|
||
}
|
||
}
|
||
</style>
|