feat: 对接项目生成配置与单集成片

This commit is contained in:
GouJ committed 2026-09-21 10:03:50 +08:00
1 parent 1bbfd30627
commit 01f274cd8e
9 files changed
+722 -8

No files matched your search

@@ -0,0 +1,254 @@
<script setup lang="ts">
import { computed, onScopeDispose, ref, watch } from 'vue'
import { NAlert, NButton, NCollapse, NCollapseItem, NFormItem, NSelect, NSpin } from 'naive-ui'
import { errorMessage } from '../../../lib/http'
import { generationConfigApi } from '../api'
import type {
CreativeProfileAspectRatio,
CreativeProfileInput,
GenerationModelCatalog,
GenerationModelItem,
GenerationOptions
} from '../types'
import GenerationOptionFields from './GenerationOptionFields.vue'
const props = defineProps<{ projectId: string }>()
const emit = defineEmits<{ saved: [profile: CreativeProfileInput] }>()
const loading = ref(false)
const saving = ref(false)
const error = ref('')
const notice = ref('')
const catalog = ref<GenerationModelCatalog | null>(null)
const profile = ref<CreativeProfileInput | null>(null)
let controller: AbortController | null = null
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
)
const imageModel = computed(() =>
enabledImages.value.find(item =>
profile.value ? item.provider === profile.value.imageProvider && item.model === profile.value.imageModel : false
)
)
const videoModel = computed(() =>
enabledVideos.value.find(item =>
profile.value ? item.provider === profile.value.videoProvider && item.model === profile.value.videoModel : false
)
)
function selectionValue(provider: string, model: string) {
return `${provider}\u0000${model}`
}
function modelOptions(items: GenerationModelItem[]) {
return items.map(item => ({
label: `${item.providerLabel} · ${item.modelLabel}`,
value: selectionValue(item.provider, item.model || '')
}))
}
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') return [[schema.key, 'auto']]
return []
})
)
}
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)
}
}
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()
loading.value = true
error.value = ''
notice.value = ''
try {
const [nextCatalog, saved] = await Promise.all([
generationConfigApi.catalog(controller.signal),
generationConfigApi.profile(props.projectId, controller.signal)
])
catalog.value = nextCatalog
profile.value = saved
? {
aspectRatio: saved.aspectRatio,
imageProvider: saved.imageProvider,
imageModel: saved.imageModel,
imageOptions: { ...saved.imageOptions },
videoProvider: saved.videoProvider,
videoModel: saved.videoModel,
videoOptions: { ...saved.videoOptions }
}
: createInitial(nextCatalog)
} catch (cause) {
if (!controller.signal.aborted) error.value = errorMessage(cause)
} finally {
if (!controller.signal.aborted) loading.value = false
}
}
async function save() {
if (!profile.value || saving.value) return
saving.value = true
error.value = ''
notice.value = ''
try {
const saved = await generationConfigApi.saveProfile(props.projectId, profile.value)
profile.value = {
aspectRatio: saved.aspectRatio,
imageProvider: saved.imageProvider,
imageModel: saved.imageModel,
imageOptions: { ...saved.imageOptions },
videoProvider: saved.videoProvider,
videoModel: saved.videoModel,
videoOptions: { ...saved.videoOptions }
}
notice.value = '项目生成配置已保存,后续首帧和视频生成会继承此配置。'
emit('saved', profile.value)
} catch (cause) {
error.value = errorMessage(cause)
} finally {
saving.value = false
}
}
watch(() => props.projectId, load, { immediate: true })
onScopeDispose(() => controller?.abort())
</script>
<template>
<NCollapse class="mt-4">
<NCollapseItem name="creative-profile" title="项目画布与生成模型">
<NSpin :show="loading">
<NAlert v-if="error" type="error" :show-icon="false" class="mb-3">{{ error }}</NAlert>
<NAlert v-if="notice" type="success" :show-icon="false" class="mb-3">{{ notice }}</NAlert>
<NAlert
v-if="!loading && (!enabledImages.length || !enabledVideos.length)"
type="warning"
:show-icon="false"
>
后端尚未启用完整的图片和视频模型,暂时无法保存项目生成配置。
</NAlert>
<div v-if="profile" class="creative-profile-grid">
<NFormItem label="作品宽高比">
<NSelect
:value="profile.aspectRatio"
:options="aspectRatios"
:disabled="saving"
@update:value="profile.aspectRatio = $event as CreativeProfileAspectRatio"
/>
</NFormItem>
<div></div>
<section class="model-section">
<NFormItem label="图片模型">
<NSelect
:value="imageSelection"
:options="modelOptions(enabledImages)"
:disabled="saving"
@update:value="selectModel('image', $event)"
/>
</NFormItem>
<GenerationOptionFields
v-model="profile.imageOptions"
:schemas="imageModel?.generationOptions || []"
:disabled="saving"
/>
</section>
<section class="model-section">
<NFormItem label="视频模型">
<NSelect
:value="videoSelection"
:options="modelOptions(enabledVideos)"
:disabled="saving"
@update:value="selectModel('video', $event)"
/>
</NFormItem>
<GenerationOptionFields
v-model="profile.videoOptions"
:schemas="videoModel?.generationOptions || []"
:disabled="saving"
/>
</section>
</div>
<div class="mt-3 flex items-center gap-3">
<NButton
type="primary"
:loading="saving"
:disabled="loading || !profile || !enabledImages.length || !enabledVideos.length"
@click="save"
>保存项目生成配置</NButton
>
<span class="text-xs text-muted">切换模型时会按新模型重置生成参数。</span>
</div>
</NSpin>
</NCollapseItem>
</NCollapse>
</template>
<style scoped>
@reference "../../../styles/styles.css";
.creative-profile-grid {
@apply grid grid-cols-[repeat(2,_minmax(0,_1fr))] gap-x-5;
}
.model-section {
@apply min-w-0 p-4 bg-(--app-subtle);
}
@media (max-width: 900px) {
.creative-profile-grid {
@apply grid-cols-[minmax(0,_1fr)];
}
.creative-profile-grid > div:empty {
@apply hidden;
}
}
</style>