From b247f96a05b4b6fefcdc9d9f2c84819514e28ea3 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Tue, 15 Sep 2026 01:09:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai-avatar):=20normalizeEmotion=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=207=20=E7=A7=8D=E6=96=B0=E6=9E=9A=E4=B8=BE+=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=97=A7=204=20=E7=A7=8D=E4=B8=8E=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/src/pages/ai-avatar/utils/contract.ts | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/apps/web/src/pages/ai-avatar/utils/contract.ts b/apps/web/src/pages/ai-avatar/utils/contract.ts index d8d361028..f660f07d6 100644 --- a/apps/web/src/pages/ai-avatar/utils/contract.ts +++ b/apps/web/src/pages/ai-avatar/utils/contract.ts @@ -6,21 +6,50 @@ */ import type { AiAvatarTitleConfig, AiAvatarCoverConfig, VoiceEmotion } from "../types" -/* ── 情绪:中文 → 英文(防御性映射;state 默认已是英文) ── */ -const EMOTION_ZH_TO_EN: Record = { - 自然: "natural", - 兴奋: "excited", - 沉稳: "calm", - 亲切: "friendly", +/* ── 情绪:中文/旧枚举 → CosyVoice 7 种英文枚举 ── */ +const EMOTION_ALIAS: Record = { + // 新英文枚举 + neutral: "neutral", + happy: "happy", + sad: "sad", + angry: "angry", + surprised: "surprised", + fearful: "fearful", + disgusted: "disgusted", + // 旧英文枚举(4 种,向前兼容) + natural: "neutral", + excited: "happy", + calm: "neutral", + friendly: "happy", + // 中文 + 自然: "neutral", + 开心: "happy", + 难过: "sad", + 生气: "angry", + 惊讶: "surprised", + 恐惧: "fearful", + 厌恶: "disgusted", + // 旧中文 + 兴奋: "happy", + 沉稳: "neutral", + 亲切: "happy", } -const VALID_EMOTIONS: VoiceEmotion[] = ["natural", "excited", "calm", "friendly"] +const VALID_EMOTIONS: VoiceEmotion[] = [ + "neutral", + "happy", + "sad", + "angry", + "surprised", + "fearful", + "disgusted", +] -/** 归一化为后端英文枚举 natural/excited/calm/friendly;非法/空值回退 natural。 */ +/** 归一化为后端英文枚举 neutral/happy/sad/angry/surprised/fearful/disgusted;非法/空值回退 neutral。 */ export function normalizeEmotion(raw: string | undefined | null): VoiceEmotion { - if (!raw) return "natural" + if (!raw) return "neutral" const v = raw.trim() if ((VALID_EMOTIONS as string[]).includes(v)) return v as VoiceEmotion - return EMOTION_ZH_TO_EN[v] ?? "natural" + return EMOTION_ALIAS[v] ?? "neutral" } /* ── 标题:前端 state → 后端 build_title_drawtext_filter 字段(单个 title_config dict) ── */