Files
short-drama-agent-front/src/features/generation-config/components/GenerationModelControls.vue
T

191 lines
7.2 KiB
Vue

<script setup lang="ts">
import { userFacingText } from '../../../lib/user-facing-text'
import { computed } from 'vue'
import { NFormItem, NInputNumber, NSelect, NSwitch, NTag } from 'naive-ui'
import FieldLabel from '../../../components/ui/FieldLabel.vue'
import type { SelectOption } from 'naive-ui'
import type {
CreativeProfileAspectRatio,
GenerationModelCatalog,
GenerationModelItem,
GenerationOptions
} from '../types'
/** 本次生成模型与参数控件,字段说明在标签图标中按需查看。 */
const props = defineProps<{
type: 'image' | 'video'
catalog: GenerationModelCatalog
provider: string
options: GenerationOptions
aspectRatio: CreativeProfileAspectRatio
disabled?: boolean
}>()
/** 模型与参数编辑通知。 */
const emit = defineEmits<{
'update:provider': [value: string]
'update:options': [value: GenerationOptions]
}>()
/** 当前已启用的媒介模型。 */
const models = computed(() =>
(props.type === 'image' ? props.catalog.images : props.catalog.videos).filter(item => item.enabled)
)
/** 本次选择的实际模型。 */
const model = computed(() => models.value.find(item => item.provider === props.provider) ?? models.value[0])
/** 可用模型的下拉选项。 */
const modelOptions = computed(() =>
models.value.map(item => ({
label: item.modelLabel ? `${item.providerLabel} · ${item.modelLabel}` : item.providerLabel,
value: item.provider
}))
)
function selectProvider(provider: string) {
const next = models.value.find(item => item.provider === provider)
if (!next) return
emit('update:provider', provider)
emit('update:options', { ...next.defaults })
}
function updateOption(key: string, value: string | number | boolean | null) {
const next = { ...props.options }
if (value === null) delete next[key]
else next[key] = value
emit('update:options', next)
}
/**
* 为模型参数生成中文选项,保留真实取值和支持的时长范围。
* @param item 当前模型参数
* @returns 参数选项
*/
function choices(item: GenerationModelItem['generationOptions'][number]): SelectOption[] {
const options = [...(item.options ?? []), ...(item.specialValues ?? [])]
.filter(option => typeof option.value === 'string' || typeof option.value === 'number')
.map(option => ({ label: userFacingText(option.label, option.label), value: option.value as string | number }))
if (
/duration|时长/i.test(`${item.key} ${item.label}`) &&
Number.isInteger(item.min) &&
Number.isInteger(item.max) &&
item.max! >= item.min! &&
item.max! - item.min! <= 60
) {
for (let value = item.min!; value <= item.max!; value++) options.push({ label: `${value} 秒`, value })
}
return options
}
function selectValue(key: string): string | number | null {
const value = props.options[key]
return typeof value === 'string' || typeof value === 'number' ? value : null
}
</script>
<template>
<div class="generation-model-controls">
<NFormItem class="generation-model-field generation-model-field-wide" label="模型">
<div class="generation-model-field-control">
<NSelect
:value="model?.provider"
:options="modelOptions"
:disabled="disabled || !modelOptions.length"
@update:value="selectProvider"
/>
</div>
</NFormItem>
<template v-if="model">
<NFormItem
v-for="item in model.generationOptions"
:key="item.key"
class="generation-model-field"
:label="userFacingText(item.label, item.label)"
:show-feedback="false"
>
<!-- 以下是生成参数标签与说明提示模块 -->
<template #label><FieldLabel :label="userFacingText(item.label, item.label)" :description="userFacingText(item.description)" /></template>
<div class="generation-model-field-control">
<div v-if="item.type === 'boolean'" class="generation-model-toggle">
<NSwitch
:value="Boolean(options[item.key])"
:disabled="disabled || item.userEditable === false"
@update:value="updateOption(item.key, $event)"
/>
<span class="text-xs">{{ options[item.key] ? '已启用' : '未启用' }}</span>
</div>
<NSelect
v-else-if="/duration|时长/i.test(`${item.key} ${item.label}`)"
:value="selectValue(item.key)"
:options="choices(item)"
:disabled="disabled || item.userEditable === false"
@update:value="updateOption(item.key, $event)"
/>
<NInputNumber
v-else-if="item.type === 'integer' || item.type === 'number'"
:value="typeof options[item.key] === 'number' ? Number(options[item.key]) : null"
:min="item.min"
:max="item.max"
:disabled="disabled || item.userEditable === false"
@update:value="updateOption(item.key, $event)"
/>
<NSelect
v-else
:value="selectValue(item.key)"
:options="choices(item)"
:disabled="disabled || item.userEditable === false"
@update:value="updateOption(item.key, $event)"
/>
</div>
</NFormItem>
<NFormItem class="generation-model-field" label="画幅比例" :show-feedback="false">
<template #label><FieldLabel label="画幅比例" description="项目画幅 · 单次生成不可覆盖" /></template>
<div class="generation-model-static">
<NTag size="small">{{ aspectRatio }}</NTag>
</div>
</NFormItem>
</template>
</div>
</template>
<style scoped>
@reference "../../../styles/styles.css";
.generation-model-controls {
@apply grid grid-cols-[repeat(2,_minmax(0,_1fr))] items-start gap-x-5 gap-y-5;
}
.generation-model-field {
@apply min-w-0 mb-0;
}
.generation-model-field-wide {
@apply col-span-2;
}
.generation-model-field :deep(.n-form-item-label) {
min-height: 27px;
padding: 0 0 7px;
font-size: 12px;
line-height: 20px;
}
.generation-model-field :deep(.n-form-item-blank) {
@apply min-h-[34px] min-w-0;
}
.generation-model-field-control {
@apply flex w-full min-w-0 flex-col items-stretch gap-1.5;
}
.generation-model-field-control :deep(.n-input-number),
.generation-model-field-control :deep(.n-select) {
@apply w-full;
}
.generation-model-toggle,
.generation-model-static {
@apply flex min-h-[34px] min-w-0 items-center gap-2.5;
}
@media (max-width: 760px) {
.generation-model-controls {
@apply grid-cols-[minmax(0,_1fr)] gap-y-4;
}
.generation-model-field-wide {
@apply col-span-1;
}
}
</style>