feat: 对接项目生成配置与单集成片
This commit is contained in:
1 parent
1bbfd30627
commit
01f274cd8e
9 files changed
+722
-8
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { request } from '../../lib/http'
|
||||
import type {
|
||||
CreativeProfile,
|
||||
CreativeProfileInput,
|
||||
GenerationMediaType,
|
||||
GenerationModelCatalog,
|
||||
GenerationOverride,
|
||||
GenerationOverrideInput,
|
||||
GenerationOverrideTargetType,
|
||||
ResolvedGenerationConfig
|
||||
} from './types'
|
||||
|
||||
function projectPath(projectId: string) {
|
||||
return `/projects/${encodeURIComponent(projectId)}`
|
||||
}
|
||||
|
||||
function overridePath(
|
||||
projectId: string,
|
||||
targetType: GenerationOverrideTargetType,
|
||||
targetId: string,
|
||||
mediaType: GenerationMediaType
|
||||
) {
|
||||
return `${projectPath(projectId)}/generation-overrides/${targetType}/${encodeURIComponent(targetId)}/${mediaType}`
|
||||
}
|
||||
|
||||
export const generationConfigApi = {
|
||||
catalog: (signal?: AbortSignal) => request<GenerationModelCatalog>('/generation-models', { signal }),
|
||||
profile: (projectId: string, signal?: AbortSignal) =>
|
||||
request<CreativeProfile | null>(`${projectPath(projectId)}/creative-profile`, { signal }),
|
||||
saveProfile: (projectId: string, input: CreativeProfileInput) =>
|
||||
request<CreativeProfile>(`${projectPath(projectId)}/creative-profile`, { method: 'PUT', body: input }),
|
||||
resolve: (
|
||||
projectId: string,
|
||||
mediaType: GenerationMediaType,
|
||||
input: {
|
||||
provider?: string
|
||||
model?: string
|
||||
options?: Record<string, unknown>
|
||||
target?: { targetType: GenerationOverrideTargetType; targetId: string }
|
||||
}
|
||||
) =>
|
||||
request<ResolvedGenerationConfig>(`${projectPath(projectId)}/generation-config/${mediaType}/resolve`, {
|
||||
method: 'POST',
|
||||
body: input
|
||||
}),
|
||||
override: (
|
||||
projectId: string,
|
||||
targetType: GenerationOverrideTargetType,
|
||||
targetId: string,
|
||||
mediaType: GenerationMediaType,
|
||||
signal?: AbortSignal
|
||||
) => request<GenerationOverride | null>(overridePath(projectId, targetType, targetId, mediaType), { signal }),
|
||||
saveOverride: (
|
||||
projectId: string,
|
||||
targetType: GenerationOverrideTargetType,
|
||||
targetId: string,
|
||||
mediaType: GenerationMediaType,
|
||||
input: GenerationOverrideInput
|
||||
) =>
|
||||
request<GenerationOverride>(overridePath(projectId, targetType, targetId, mediaType), {
|
||||
method: 'PUT',
|
||||
body: input
|
||||
}),
|
||||
deleteOverride: (
|
||||
projectId: string,
|
||||
targetType: GenerationOverrideTargetType,
|
||||
targetId: string,
|
||||
mediaType: GenerationMediaType
|
||||
) => request<unknown>(overridePath(projectId, targetType, targetId, mediaType), { method: 'DELETE' })
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,85 @@
|
||||
<script setup lang="ts">
|
||||
import { NCheckbox, NFormItem, NInput, NInputNumber, NSelect } from 'naive-ui'
|
||||
import type { GenerationOptionSchema, GenerationOptions } from '../types'
|
||||
|
||||
defineProps<{ schemas: GenerationOptionSchema[]; modelValue: GenerationOptions; disabled?: boolean }>()
|
||||
const emit = defineEmits<{ 'update:modelValue': [value: GenerationOptions] }>()
|
||||
|
||||
function update(model: GenerationOptions, key: string, value: string | number | boolean | null) {
|
||||
const next = { ...model }
|
||||
if (value === null || value === '') delete next[key]
|
||||
else next[key] = value
|
||||
emit('update:modelValue', next)
|
||||
}
|
||||
|
||||
function selectValue(value: string | number | boolean | undefined) {
|
||||
return typeof value === 'string' || typeof value === 'number' ? value : null
|
||||
}
|
||||
|
||||
function selectOptions(schema: GenerationOptionSchema) {
|
||||
return [...(schema.options || []), ...(schema.specialValues || [])].flatMap(option =>
|
||||
typeof option.value === 'string' || typeof option.value === 'number'
|
||||
? [{ label: option.label, value: option.value }]
|
||||
: []
|
||||
)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="schemas.length" class="generation-option-grid">
|
||||
<NFormItem
|
||||
v-for="schema in schemas"
|
||||
:key="schema.key"
|
||||
:label="`${schema.label}${schema.required ? ' *' : ''}`"
|
||||
:feedback="schema.description"
|
||||
:show-feedback="!!schema.description"
|
||||
>
|
||||
<NCheckbox
|
||||
v-if="schema.type === 'boolean'"
|
||||
:checked="modelValue[schema.key] === true"
|
||||
:disabled="disabled"
|
||||
@update:checked="update(modelValue, schema.key, $event)"
|
||||
>启用</NCheckbox
|
||||
>
|
||||
<NSelect
|
||||
v-else-if="schema.type === 'select'"
|
||||
:value="selectValue(modelValue[schema.key])"
|
||||
:options="selectOptions(schema)"
|
||||
:disabled="disabled"
|
||||
clearable
|
||||
@update:value="update(modelValue, schema.key, $event)"
|
||||
/>
|
||||
<NInputNumber
|
||||
v-else-if="schema.type === 'integer' || schema.type === 'number'"
|
||||
:value="typeof modelValue[schema.key] === 'number' ? (modelValue[schema.key] as number) : null"
|
||||
:min="schema.min"
|
||||
:max="schema.max"
|
||||
:precision="schema.type === 'integer' ? 0 : undefined"
|
||||
:disabled="disabled"
|
||||
clearable
|
||||
@update:value="update(modelValue, schema.key, $event)"
|
||||
/>
|
||||
<NInput
|
||||
v-else
|
||||
:value="typeof modelValue[schema.key] === 'string' ? (modelValue[schema.key] as string) : ''"
|
||||
:disabled="disabled"
|
||||
placeholder="例如 2048x2048 或 auto"
|
||||
clearable
|
||||
@update:value="update(modelValue, schema.key, $event)"
|
||||
/>
|
||||
</NFormItem>
|
||||
</div>
|
||||
<p v-else class="text-xs text-muted">当前模型没有开放额外生成参数。</p>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
@reference "../../../styles/styles.css";
|
||||
.generation-option-grid {
|
||||
@apply grid grid-cols-[repeat(2,_minmax(0,_1fr))] gap-x-4;
|
||||
}
|
||||
@media (max-width: 760px) {
|
||||
.generation-option-grid {
|
||||
@apply grid-cols-[minmax(0,_1fr)];
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,80 @@
|
||||
export type CreativeProfileAspectRatio = '9:16' | '16:9' | '1:1' | '4:3' | '3:4'
|
||||
|
||||
export type GenerationOptionValue = string | number | boolean
|
||||
export type GenerationOptions = Record<string, GenerationOptionValue>
|
||||
|
||||
export interface GenerationOptionChoice {
|
||||
label: string
|
||||
value: GenerationOptionValue
|
||||
}
|
||||
|
||||
export interface GenerationOptionSchema {
|
||||
key: string
|
||||
label: string
|
||||
type: 'boolean' | 'integer' | 'number' | 'select' | 'size'
|
||||
required: boolean
|
||||
options?: GenerationOptionChoice[]
|
||||
min?: number
|
||||
max?: number
|
||||
specialValues?: GenerationOptionChoice[]
|
||||
providerDefault?: GenerationOptionValue
|
||||
description?: string
|
||||
}
|
||||
|
||||
export interface GenerationModelItem {
|
||||
type: 'image' | 'video'
|
||||
provider: string
|
||||
providerLabel: string
|
||||
model: string | null
|
||||
modelLabel: string
|
||||
supported: boolean
|
||||
enabled: boolean
|
||||
isDefault: boolean
|
||||
capabilities: Record<string, unknown>
|
||||
generationOptions: GenerationOptionSchema[]
|
||||
}
|
||||
|
||||
export interface GenerationModelCatalog {
|
||||
defaults: { imageProvider: string; videoProvider: string }
|
||||
images: GenerationModelItem[]
|
||||
videos: GenerationModelItem[]
|
||||
}
|
||||
|
||||
export interface CreativeProfileInput {
|
||||
aspectRatio: CreativeProfileAspectRatio
|
||||
imageProvider: string
|
||||
imageModel: string
|
||||
imageOptions: GenerationOptions
|
||||
videoProvider: string
|
||||
videoModel: string
|
||||
videoOptions: GenerationOptions
|
||||
}
|
||||
|
||||
export interface CreativeProfile extends CreativeProfileInput {
|
||||
id?: string
|
||||
projectId: string
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
export interface ResolvedGenerationConfig {
|
||||
provider: string
|
||||
model: string
|
||||
options: GenerationOptions
|
||||
}
|
||||
|
||||
export type GenerationOverrideTargetType = 'shot' | 'asset'
|
||||
export type GenerationMediaType = 'image' | 'video'
|
||||
|
||||
export interface GenerationOverrideInput {
|
||||
provider?: string | null
|
||||
model?: string | null
|
||||
options: GenerationOptions
|
||||
}
|
||||
|
||||
export interface GenerationOverride extends GenerationOverrideInput {
|
||||
projectId: string
|
||||
targetType: GenerationOverrideTargetType
|
||||
targetId: string
|
||||
mediaType: GenerationMediaType
|
||||
}
|
||||
Reference in new issue
Block a user