fix: 自建情景在聊天面板与系统消息中正确显示图标和名称

This commit is contained in:
2026-06-22 16:02:02 +08:00
parent f1b9e4fcc1
commit 79d321c78a
2 changed files with 30 additions and 7 deletions

View File

@@ -21,6 +21,15 @@ import { loadConfig, loadTheme, saveTheme } from "./lib/storage";
import { I18nContext, parseLocale, t } from "./lib/i18n"; import { I18nContext, parseLocale, t } from "./lib/i18n";
import { renderScenarioIconById } from "./lib/scenarioIcons"; import { renderScenarioIconById } from "./lib/scenarioIcons";
import type { Locale } from "./lib/i18n"; import type { Locale } from "./lib/i18n";
/** SVG 图标 ID → emoji 映射(用于纯文本场景,如系统消息) */
const ICON_ID_TO_EMOJI: Record<string, string> = {
sparkles: "✨", theater: "🎭", palette: "🎨", target: "🎯", film: "🎬",
"book-open": "📖", book: "📚", edit: "✏️", clipboard: "📋", "map-pin": "📍",
search: "🔍", lightbulb: "💡", briefcase: "💼", code: "💻", "graduation-cap": "🎓",
mic: "🎤", music: "🎵", globe: "🌐", heart: "❤️", shield: "🛡️",
zap: "⚡", coffee: "☕", tool: "🔧", "message-circle": "💬",
};
import type { Theme } from "./types"; import type { Theme } from "./types";
import type { UserScenario } from "./lib/api/scenarios"; import type { UserScenario } from "./lib/api/scenarios";
import "./App.css"; import "./App.css";
@@ -228,10 +237,14 @@ function AppContent() {
} }
} }
updateConfig(updates); updateConfig(updates);
// 插入系统提示消息 // 插入系统提示消息(自建情景的 icon 是 SVG ID需转为 emoji
const scenarioName = sc const iconText = sc
? `${sc.icon} ${sc.nameKey ? tr(sc.nameKey) : sc.name}` ? (ICON_ID_TO_EMOJI[sc.icon] || sc.icon)
: "";
const displayName = sc
? (sc.nameKey ? tr(sc.nameKey) : sc.name)
: scenarioId; : scenarioId;
const scenarioName = iconText ? `${iconText} ${displayName}` : displayName;
setMessages(prev => [...prev, { setMessages(prev => [...prev, {
id: uuidv4(), id: uuidv4(),
role: "system", role: "system",
@@ -607,6 +620,7 @@ function AppContent() {
currentReply={currentReply} currentReply={currentReply}
connectionStatus={connectionStatus} connectionStatus={connectionStatus}
currentScenario={config.scenario} currentScenario={config.scenario}
allScenarios={allScenarios}
isProcessing={isProcessing} isProcessing={isProcessing}
isVADReady={isVADReady} isVADReady={isVADReady}
vadError={vadError ?? undefined} vadError={vadError ?? undefined}

View File

@@ -7,6 +7,7 @@
import { useEffect, useRef, useState, type ReactNode } from "react"; import { useEffect, useRef, useState, type ReactNode } from "react";
import { useI18n } from "../../lib/i18n"; import { useI18n } from "../../lib/i18n";
import { scenarios } from "../../lib/scenarios"; import { scenarios } from "../../lib/scenarios";
import type { ExtendedScenario } from "../../hooks/useScenarios";
import type { ChatMessage } from "../../types"; import type { ChatMessage } from "../../types";
import type { ConnectionStatus } from "../../lib/websocket"; import type { ConnectionStatus } from "../../lib/websocket";
@@ -15,6 +16,7 @@ interface ChatPanelProps {
currentReply?: string; currentReply?: string;
connectionStatus: ConnectionStatus; connectionStatus: ConnectionStatus;
currentScenario?: string; currentScenario?: string;
allScenarios?: ExtendedScenario[];
isProcessing?: boolean; isProcessing?: boolean;
isVADReady?: boolean; isVADReady?: boolean;
vadError?: string | null; vadError?: string | null;
@@ -43,6 +45,7 @@ export function ChatPanel({
currentReply, currentReply,
connectionStatus, connectionStatus,
currentScenario, currentScenario,
allScenarios,
isProcessing, isProcessing,
isVADReady, isVADReady,
vadError, vadError,
@@ -103,6 +106,10 @@ export function ChatPanel({
} }
}; };
// 当前情景对象(优先从 allScenarios 查找,含自建情景;兜底用系统情景)
const scenarioList = allScenarios || scenarios;
const activeScenarioObj = scenarioList.find(sc => sc.id === activeScenario);
return ( return (
<div className="chat-panel"> <div className="chat-panel">
<div className="chat-panel__messages" ref={containerRef}> <div className="chat-panel__messages" ref={containerRef}>
@@ -111,11 +118,13 @@ export function ChatPanel({
<div className="chat-panel__scenario-hint"> <div className="chat-panel__scenario-hint">
<div className="scenario-hint-card"> <div className="scenario-hint-card">
<span className="scenario-hint-card__icon"> <span className="scenario-hint-card__icon">
{scenarios.find(s => s.id === activeScenario)?.icon} {activeScenarioObj?.icon}
</span> </span>
<div className="scenario-hint-card__text"> <div className="scenario-hint-card__text">
<strong>{t(scenarios.find(s => s.id === activeScenario)?.nameKey || "")}</strong> <strong>{activeScenarioObj ? (activeScenarioObj.nameKey ? t(activeScenarioObj.nameKey) : activeScenarioObj.name) : ""}</strong>
<p>{t(`scenario.${activeScenario}.hint`)}</p> {activeScenarioObj && (activeScenarioObj.descKey || activeScenarioObj.description) && (
<p>{activeScenarioObj.descKey ? t(activeScenarioObj.descKey) : activeScenarioObj.description}</p>
)}
</div> </div>
</div> </div>
</div> </div>
@@ -124,7 +133,7 @@ export function ChatPanel({
{/* 空状态:欢迎信息 */} {/* 空状态:欢迎信息 */}
{isEmpty && ( {isEmpty && (
<div className="chat-panel__welcome"> <div className="chat-panel__welcome">
<span className="chat-panel__welcome-icon">{scenarios.find(s => s.id === activeScenario)?.icon || <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>}</span> <span className="chat-panel__welcome-icon">{activeScenarioObj?.icon || <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>}</span>
<p>{isConnected ? t("chat.welcome.prompt") : t("chat.empty.prompt")}</p> <p>{isConnected ? t("chat.welcome.prompt") : t("chat.empty.prompt")}</p>
<span className="chat-panel__welcome-hint">{isConnected ? t("chat.welcome.hint") : t("chat.empty.hint")}</span> <span className="chat-panel__welcome-hint">{isConnected ? t("chat.welcome.hint") : t("chat.empty.hint")}</span>