/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { optionKey, optionValue } from './constraints'; // 已知参数 key 的中文标签(作为 i18n key)。schema 下发了 label 时优先用 label。 export const PARAM_LABEL_KEYS = { aspect_ratio: '画面比例', ratio: '画面比例', resolution: '分辨率', duration: '时长', generate_audio: '生成音频', audio: '生成音频', seed: '随机种子', watermark: '水印', return_last_frame: '返回尾帧', camera_motion: '运镜', camera_fixed: '固定镜头', negative_prompt: '反向提示词', prompt_extend: '智能扩写提示词', quality: '画质', n: '生成数量', size: '尺寸', style: '风格', mode: '生成模式', }; /** 参数显示名。 */ export function paramLabel(t, param) { if (!param) return ''; if (param.label) return t(param.label); const k = PARAM_LABEL_KEYS[param.key]; return k ? t(k) : param.key; } /** 单个选项的显示文本:对象选项取 label,否则拼接单位(5 + "s" → "5s")。 */ export function optionLabel(t, param, opt) { if (opt && typeof opt === 'object' && opt.label) return t(opt.label); const v = optionValue(opt); if (typeof v === 'boolean') return v ? t('是') : t('否'); const unit = param && param.unit ? param.unit : ''; return `${v}${unit}`; } function describeWhen(t, when, params) { const byKey = {}; (params || []).forEach((p) => { if (p && p.key) byKey[p.key] = p; }); return Object.entries(when || {}) .map(([k, v]) => { const p = byKey[k] || { key: k }; const vs = Array.isArray(v) ? v : [v]; return `${paramLabel(t, p)} ${vs.map((x) => optionLabel(t, p, x)).join('/')}`; }) .join('、'); } /** * 约束原因 → 一句人话,如"分辨率 1080P 时,时长仅支持 5s"。 * reason 为 applyConstraints 返回的 { when, allowed, message? }。 */ export function formatConstraintReason(t, reason, param, params) { if (!reason) return ''; if (reason.message) return t(reason.message); const allowed = (reason.allowed || []) .map((v) => optionLabel(t, param, v)) .join(' / '); return t('{{cond}} 时,{{name}}仅支持 {{allowed}}', { cond: describeWhen(t, reason.when, params), name: paramLabel(t, param), allowed, }); } /** 判断选项是否被禁用;返回原因文本或空串。 */ export function disabledReasonOf(t, disabledMap, opt, param, params) { if (!disabledMap) return ''; const r = disabledMap[optionKey(opt)]; return r ? formatConstraintReason(t, r, param, params) : ''; }