Files
monkey-ai-web/src/components/playground/ChatArea.jsx
T

1028 lines
45 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import {
EyeOff as ActionEyeOff,
Eye as ActionEye,
X as ActionX,
ArrowDown as ActionArrowDown,
} from '@/components/common/icons/ActionIcons';
import React, { useContext } from 'react';
import {
Card,
Chat,
Typography,
Button,
Select,
Toast,
Tooltip,
Popover,
} from '@/components/ui';
import {
MessageSquare,
Eye,
Sparkles,
Brain,
Code2,
Lightbulb,
Zap,
Bot,
Wand2,
ChevronDown,
MessageCircle,
Layers,
Paintbrush,
Upload,
ListChecks,
} from 'lucide-react';
import { useChatScroll } from '../../hooks/playground/useChatScroll';
import { useTranslation } from 'react-i18next';
import CustomInputRender from './CustomInputRender';
import MessageSelectionBar from './MessageSelectionBar';
import {
getExperienceCenterBool,
getExperienceCenterPositiveInt,
getExperienceCenterValue,
isModelFilesCapable,
isModelVisionAllowed,
selectFilter,
countRounds,
} from '../../helpers';
import { usePlayground } from '../../context/Playground';
import { StatusContext } from '../../context/Status';
const ChatArea = ({
chatRef,
message,
activeSessionId,
inputs,
styleState,
showDebugPanel,
roleInfo,
onMessageSend,
onMessageCopy,
onMessageReset,
onMessageDelete,
onStopGenerator,
onClearMessages,
onToggleDebugPanel,
renderCustomChatContent,
renderChatBoxAction,
models = [],
groups = [],
onInputChange,
onCreateSession,
sessionCreating = false,
onOpenSessionDrawer,
// 多选导出
selectionMode = false,
selectedCount = 0,
selectableCount = 0,
isAllSelected = false,
onEnterSelectionMode,
onExitSelectionMode,
onSelectAll,
onClearSelection,
onExportSelected,
// 长会话摘要管理
conversationSummary = '',
onRegenerateSummary,
onClearSummary,
}) => {
const { t } = useTranslation();
const {
imageGenMode,
imageGenModel,
imageGenSize,
onImageGenModelChange,
onImageGenSizeChange,
onUploadFiles,
currentModel,
isVisionAllowed,
} = usePlayground();
// 滚动到底部跟随 —— 流式时如果用户已经滚上去看历史,显示悬浮"回到最新"按钮
const chatWrapperRef = React.useRef(null);
const { isAtBottom, scrollToBottom } = useChatScroll(chatWrapperRef, [
message.length,
activeSessionId,
]);
// 多选模式下底部悬浮条占据同一位置,隐藏「回到最新」避免视觉重叠
const showScrollToBottom =
!isAtBottom && message.length > 0 && !selectionMode;
const [statusState] = useContext(StatusContext);
const status = statusState?.status;
const [isDragOver, setIsDragOver] = React.useState(false);
// 摘要 chip 的本地 loading:调用 onRegenerateSummary / onClearSummary(Promise)期间 disable 按钮
const [summaryActionLoading, setSummaryActionLoading] = React.useState(null); // 'regen' | 'clear' | null
// Feature C:长会话引导 + 摘要状态指示,使用同一条悬浮条。
// 行为:
// - 有摘要 → 持续显示「📝 已压缩较早对话」chip,含 查看/重新摘要/清空 actions
// - 无摘要 + 轮数到阈值 → 显示「建议新建对话」可关闭横幅
// - 两者互斥;摘要存在时,新建对话的劝阻让位给摘要管理
const [longConvHintDismissed, setLongConvHintDismissed] =
React.useState(false);
React.useEffect(() => {
// 切换会话时重置「已忽略」状态
setLongConvHintDismissed(false);
}, [activeSessionId]);
const ecSummaryThreshold = getExperienceCenterPositiveInt(
status,
'experience_center_summary_threshold',
'experience_center_summary_threshold',
0,
);
const longConvHintThreshold =
ecSummaryThreshold > 0 ? ecSummaryThreshold : 20;
const conversationRounds = countRounds(message);
const hasSummary = !!(conversationSummary && conversationSummary.trim());
// 摘要 chip:摘要存在就常驻显示,不可关闭(信息性指示)
const showSummaryChip =
styleState.isExperienceMode &&
!!activeSessionId &&
!imageGenMode &&
hasSummary;
// 长对话提示:摘要存在时不再劝新建对话(互斥),未启用摘要时按阈值显示
const showLongConversationHint =
styleState.isExperienceMode &&
!!activeSessionId &&
!imageGenMode &&
!hasSummary &&
!longConvHintDismissed &&
conversationRounds >= longConvHintThreshold;
const ecEnableDragUpload =
styleState.isExperienceMode &&
getExperienceCenterBool(
status,
'experience_center_enable_drag_upload',
'experience_center_enable_drag_upload',
true,
) &&
getExperienceCenterBool(
status,
'experience_center_enable_file_upload',
'experience_center_enable_file_upload',
false,
);
// 绘图模式根本不读 uploadedFiles,所以拖拽 overlay 直接屏蔽,避免误操作。
// 注意:dragUploadAvailable 必须在 ecEnableDragUpload 声明之后,否则 production
// minify 后会触发 const TDZ —— "Cannot access X before initialization"
const dragUploadAvailable = ecEnableDragUpload && !imageGenMode;
const ecUploadMaxSizeMB = getExperienceCenterPositiveInt(
status,
'experience_center_upload_max_size',
'experience_center_upload_max_size',
10,
);
// 视觉模型白名单:用于在模型下拉选项里给视觉模型挂一枚 👁️ 徽标,
// 让用户在「选模型」环节就能预判哪个能传图片
const ecVisionModels = getExperienceCenterValue(
status,
'experience_center_vision_models',
'experience_center_vision_models',
'',
);
// 判断给定模型在当前配置下是否「可接收图片」:
// - 视觉白名单非空:仅白名单内为 true
// - 视觉白名单留空(默认):用反向黑名单(isModelFilesCapable)做启发式判断,
// gpt-3.5 / o1-mini / davinci 等老式纯文本模型会被排除
const isModelVisionCapable = React.useCallback(
(modelName) =>
isModelFilesCapable(modelName) &&
isModelVisionAllowed(modelName, ecVisionModels),
[ecVisionModels],
);
const handleDragOver = React.useCallback(
(e) => {
if (!dragUploadAvailable) return;
// 仅当拖拽里确实带文件才进入 drop 高亮,避免选中文本拖拽时也亮起
const hasFiles = Array.from(e.dataTransfer?.types || []).includes(
'Files',
);
if (!hasFiles) return;
e.preventDefault();
e.stopPropagation();
setIsDragOver(true);
},
[dragUploadAvailable],
);
const handleDragLeave = React.useCallback((e) => {
e.preventDefault();
e.stopPropagation();
if (e.currentTarget.contains(e.relatedTarget)) return;
setIsDragOver(false);
}, []);
// 拖拽和点击上传共用 useFileUpload Hook — 进度、并行、失败重试、5xx 回退逻辑一致。
// 模型能力 gate:当前模型若已知不支持文件,拖拽也要拒绝,给出与按钮 Tooltip 一致的提示。
const handleDrop = React.useCallback(
(e) => {
e.preventDefault();
e.stopPropagation();
setIsDragOver(false);
if (!dragUploadAvailable) return;
const files = e.dataTransfer?.files;
if (!files || files.length === 0) return;
if (!isModelFilesCapable(currentModel)) {
Toast.warning({
content: t(
'当前模型 {{model}} 不支持文件输入,请切换到 GPT-4o / Claude / Gemini 等多模态模型',
{ model: currentModel },
),
duration: 3,
});
return;
}
// 视觉模型白名单:当前模型未在白名单内时,过滤掉拖入的 image/* 文件,
// 非图片文件(PDF / Word / 文本)正常上传 —— 视觉白名单只限制图片输入
let accepted = Array.from(files);
if (!isVisionAllowed) {
const rejected = accepted.filter((f) => f.type?.startsWith('image/'));
accepted = accepted.filter((f) => !f.type?.startsWith('image/'));
if (rejected.length > 0) {
Toast.warning({
content: t(
'当前模型 {{model}} 未启用图片输入,已忽略 {{count}} 个图片文件',
{ model: currentModel, count: rejected.length },
),
duration: 3,
});
}
}
if (accepted.length > 0 && typeof onUploadFiles === 'function') {
onUploadFiles(accepted);
}
},
[dragUploadAvailable, onUploadFiles, currentModel, isVisionAllowed, t],
);
const ecImageGenModels = React.useMemo(
() =>
(localStorage.getItem('experience_center_image_gen_models') || 'dall-e-3')
.split(',')
.map((m) => m.trim())
.filter(Boolean)
.map((m) => ({ label: m, value: m })),
[],
);
React.useEffect(() => {
if (!styleState.isExperienceMode || ecImageGenModels.length === 0) {
return;
}
if (!ecImageGenModels.some((option) => option.value === imageGenModel)) {
onImageGenModelChange(ecImageGenModels[0].value);
}
}, [
ecImageGenModels,
imageGenModel,
onImageGenModelChange,
styleState.isExperienceMode,
]);
const imageSizeOptions = React.useMemo(
() => [
{ label: '1024x1024', value: '1024x1024' },
{ label: '1024x1792', value: '1024x1792' },
{ label: '1792x1024', value: '1792x1024' },
],
[],
);
const hasActiveSession = !styleState.isExperienceMode || !!activeSessionId;
const chatPlaceholder = hasActiveSession
? t('请输入您的问题...')
: t('请先新建或选择会话');
const renderInputArea = React.useCallback((props) => {
return <CustomInputRender {...props} />;
}, []);
// 功能卡片数据
const featureCards = [
{
icon: Lightbulb,
title: t('智能问答'),
description: t('快速获取准确答案'),
gradient: 'from-amber-400 to-orange-500',
bgLight: 'bg-amber-50',
textColor: 'text-amber-600',
},
{
icon: Wand2,
title: t('创意写作'),
description: t('激发灵感辅助创作'),
gradient: 'from-pink-400 to-rose-500',
bgLight: 'bg-pink-50',
textColor: 'text-pink-600',
},
{
icon: Code2,
title: t('代码辅助'),
description: t('生成优化代码片段'),
gradient: 'from-emerald-400 to-teal-500',
bgLight: 'bg-emerald-50',
textColor: 'text-emerald-600',
},
{
icon: Brain,
title: t('深度分析'),
description: t('复杂问题深入解读'),
gradient: 'from-brand-from to-brand-to',
bgLight: 'bg-brand/5',
textColor: 'text-brand-deep',
},
];
// 快速提问建议
const quickPrompts = [
t('帮我写一篇关于人工智能的文章'),
t('解释一下量子计算的原理'),
t('用Python写一个排序算法'),
t('给我一些提高工作效率的建议'),
];
return (
<div
ref={chatWrapperRef}
// min-h-0 必须有:本节点是父级 flex-col 的项目,默认 min-height:auto 会被
// 消息列表的 min-content 高度撑住而无法收缩。父级同列还有「会话入口条」和
// 归档横幅两个 shrink-0 项目,一旦对话超过一屏,多出来的高度会把输入区
// 顶出父容器的 overflow-hidden,底部工具按钮行被裁掉一半点不到。
className='h-full min-h-0 relative'
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
{/* 拖拽上传遮罩 — 体验中心唯一拖拽入口;点选上传走 CustomInputRender */}
{isDragOver && dragUploadAvailable && (
<div className='absolute inset-0 z-50 flex items-center justify-center bg-blue-500/10 dark:bg-blue-500/20 backdrop-blur-sm border-2 border-dashed border-blue-400 dark:border-blue-500 rounded-2xl transition-all'>
<div className='flex flex-col items-center gap-2 px-6 py-4 rounded-2xl bg-white/70 dark:bg-neutral-900/70 shadow-lg'>
<Upload
size={48}
className='text-blue-500 dark:text-blue-400 animate-bounce'
/>
<span className='text-lg font-semibold text-blue-600 dark:text-blue-400'>
{t('释放上传文件')}
</span>
<span className='text-xs text-gray-500 dark:text-gray-400'>
{t('单文件最大 {{size}}MB,支持图片 / PDF / Office 文档', {
size: ecUploadMaxSizeMB,
})}
</span>
</div>
</div>
)}
<Card
className={`h-full min-h-0 ${styleState.isExperienceMode ? 'border-none shadow-none bg-transparent' : ''}`}
bordered={false}
style={{
background: styleState.isExperienceMode ? 'transparent' : undefined,
backgroundColor: styleState.isExperienceMode
? 'transparent'
: undefined,
}}
bodyStyle={{
padding: 0,
height: '100%',
display: 'flex',
flexDirection: 'column',
overflow: 'hidden',
minHeight: 0,
background: styleState.isExperienceMode ? 'transparent' : undefined,
backgroundColor: styleState.isExperienceMode
? 'transparent'
: undefined,
}}
>
{/* 聊天头部 */}
{styleState.isMobile ? (
// 手机端头部:分组 / 模型选择器。
// 此前这里只是一个 pt-4 空占位——分组与模型 Select 全在 !isMobile 分支里,
// 而体验模式下设置面板被 !isExperienceMode 关掉、设置浮button 也被
// hideSettingsButton 隐藏,导致手机上根本没有任何入口切换模型,锁死在默认值。
<div className='flex items-center gap-2 px-3 pt-2 pb-1 overflow-x-auto scrollbar-hide'>
{groups.length > 0 &&
getExperienceCenterBool(
status,
'experience_center_enable_group_select',
'experience_center_enable_group_select',
true,
) && (
<Select
placeholder={t('选择分组')}
value={inputs.group}
onChange={(value) =>
onInputChange && onInputChange('group', value)
}
optionList={groups}
filter={selectFilter}
autoClearSearchValue={false}
dropdownClassName='model-select-dropdown'
dropdownStyle={{ maxWidth: 'calc(100vw - 24px)' }}
triggerRender={() => (
<div className='flex items-center gap-1.5 shrink-0 cursor-pointer px-2.5 py-1.5 rounded-full bg-white/90 dark:bg-neutral-800/90 backdrop-blur border border-gray-200/80 dark:border-neutral-700/80 shadow-sm active:scale-95 transition'>
<Layers size={13} className='text-blue-500 shrink-0' />
<span className='text-xs font-medium text-gray-700 dark:text-gray-200 truncate max-w-[84px]'>
{inputs.group
? groups.find((g) => g.value === inputs.group)
?.label || inputs.group
: t('选择分组')}
</span>
<ChevronDown
size={12}
className='text-gray-400 shrink-0'
/>
</div>
)}
/>
)}
<Select
placeholder={t('选择模型')}
value={inputs.model}
onChange={(value) =>
onInputChange && onInputChange('model', value)
}
optionList={models}
filter={selectFilter}
autoClearSearchValue={false}
dropdownClassName='model-select-dropdown'
dropdownStyle={{ maxWidth: 'calc(100vw - 24px)' }}
triggerRender={() => (
<div className='flex items-center gap-1.5 shrink-0 cursor-pointer px-2.5 py-1.5 rounded-full bg-white/90 dark:bg-neutral-800/90 backdrop-blur border border-gray-200/80 dark:border-neutral-700/80 shadow-sm active:scale-95 transition'>
<Bot size={13} className='text-brand shrink-0' />
<span className='text-xs font-medium text-gray-700 dark:text-gray-200 truncate max-w-[130px]'>
{inputs.model
? inputs.model.split('/').pop()
: t('选择模型')}
</span>
<ChevronDown size={12} className='text-gray-400 shrink-0' />
</div>
)}
/>
</div>
) : styleState.isExperienceMode ? (
// 体验模式下的简化头部 - 与下方聊天区域融为一体
<div className='flex items-center justify-start px-6 py-3 relative z-20'>
{/* 左对齐:分组选择器 + 模型选择器 */}
<div className='flex items-center gap-3'>
{/* 分组选择器 - 受管理员配置控制 */}
{groups.length > 0 &&
getExperienceCenterBool(
status,
'experience_center_enable_group_select',
'experience_center_enable_group_select',
true,
) && (
<>
<span className='text-sm text-gray-400 dark:text-gray-500 font-medium tracking-wide'>
{t('分组')}
</span>
<Select
placeholder={t('选择分组')}
value={inputs.group}
onChange={(value) =>
onInputChange && onInputChange('group', value)
}
optionList={groups}
filter={selectFilter}
autoClearSearchValue={false}
style={{ width: 'auto' }}
className='model-select-experience'
dropdownClassName='model-select-dropdown'
dropdownStyle={{ minWidth: 200 }}
triggerRender={() => (
<div className='flex items-center gap-2.5 cursor-pointer group/select px-4 py-2 rounded-xl bg-white/80 dark:bg-neutral-800/80 backdrop-blur-md border border-gray-200/60 dark:border-neutral-700/60 shadow-[0_2px_8px_rgba(0,0,0,0.04)] hover:shadow-[0_4px_16px_rgba(139,92,246,0.12)] hover:border-brand-from/80 dark:hover:border-brand-deep/60 transition-all duration-300'>
<div className='w-6 h-6 rounded-lg bg-gradient-to-br from-blue-500 to-cyan-600 flex items-center justify-center shadow-sm shadow-blue-500/30 flex-shrink-0'>
<Layers size={13} className='text-white' />
</div>
<span className='text-sm font-medium text-gray-700 dark:text-gray-200 group-hover/select:text-blue-600 dark:group-hover/select:text-blue-400 transition-colors truncate max-w-[120px]'>
{inputs.group
? groups.find((g) => g.value === inputs.group)
?.label || inputs.group
: t('选择分组')}
</span>
<ChevronDown
size={14}
className='text-gray-400 group-hover/select:text-blue-500 transition-all duration-300 flex-shrink-0 group-hover/select:translate-y-0.5 ml-1'
/>
</div>
)}
/>
</>
)}
<span className='text-sm text-gray-400 dark:text-gray-500 font-medium tracking-wide'>
{t('当前模型')}
</span>
<Select
placeholder={t('选择模型')}
value={inputs.model}
onChange={(value) =>
onInputChange && onInputChange('model', value)
}
optionList={models}
filter={selectFilter}
autoClearSearchValue={false}
style={{ width: 'auto' }}
className='model-select-experience'
dropdownClassName='model-select-dropdown'
dropdownStyle={{ minWidth: 340 }}
renderOptionItem={(renderProps) => {
const { value, label, focused, selected, onClick } =
renderProps;
const visionCapable = isModelVisionCapable(value);
return (
<div
onClick={onClick}
className={`flex items-center gap-2 px-3 py-2 cursor-pointer text-sm ${
focused
? 'bg-brand/5 dark:bg-brand-deep/30'
: selected
? 'bg-brand/60 dark:bg-brand-deep/40'
: 'hover:bg-gray-50 dark:hover:bg-neutral-800'
}`}
>
<span className='flex-1 truncate text-gray-700 dark:text-gray-200'>
{label || value}
</span>
{visionCapable && (
<Tooltip content={t('支持图片输入')} position='left'>
<span className='flex-shrink-0 inline-flex items-center justify-center w-5 h-5 rounded-md bg-brand/5 dark:bg-brand-deep/40 text-brand'>
<Eye size={12} />
</span>
</Tooltip>
)}
</div>
);
}}
triggerRender={() => (
<div className='flex items-center gap-2.5 cursor-pointer group/select px-4 py-2 rounded-xl bg-white/80 dark:bg-neutral-800/80 backdrop-blur-md border border-gray-200/60 dark:border-neutral-700/60 shadow-[0_2px_8px_rgba(0,0,0,0.04)] hover:shadow-[0_4px_16px_rgba(139,92,246,0.12)] hover:border-brand-from/80 dark:hover:border-brand-deep/60 transition-all duration-300'>
<div className='w-6 h-6 rounded-lg bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center shadow-sm shadow-brand/30 flex-shrink-0'>
<Bot size={13} className='text-white' />
</div>
<span className='text-sm font-medium text-gray-700 dark:text-gray-200 group-hover/select:text-brand-deep dark:group-hover/select:text-brand-from transition-colors truncate max-w-[200px]'>
{inputs.model
? inputs.model.split('/').pop()
: t('选择模型')}
</span>
<ChevronDown
size={14}
className='text-gray-400 group-hover/select:text-brand transition-all duration-300 flex-shrink-0 group-hover/select:translate-y-0.5 ml-1'
/>
</div>
)}
/>
{/* 多选导出入口:仿模型选择 pill 的玻璃拟态风格,仅在非多选态且会话非空时显示 */}
{!selectionMode && message.length > 0 && onEnterSelectionMode && (
<button
type='button'
onClick={onEnterSelectionMode}
aria-label={t('多选导出')}
className='app-action-button flex items-center gap-2 cursor-pointer px-4 py-2 rounded-xl bg-white/80 dark:bg-neutral-800/80 backdrop-blur-md border border-gray-200/60 dark:border-neutral-700/60 shadow-[0_2px_8px_rgba(0,0,0,0.04)] hover:shadow-[0_4px_16px_rgba(139,92,246,0.12)] hover:border-brand-from/80 dark:hover:border-brand-deep/60 transition-all duration-300'
>
<ListChecks size={14} className='text-brand flex-shrink-0' />
<span className='text-sm font-medium text-gray-700 dark:text-gray-200'>
{t('多选导出')}
</span>
</button>
)}
</div>
</div>
) : (
<div className='px-6 py-4 bg-gradient-to-r from-brand-from to-blue-500 rounded-t-2xl'>
<div className='flex items-center justify-between'>
<div className='flex items-center gap-3'>
<div className='w-10 h-10 rounded-full bg-white/20 backdrop-blur flex items-center justify-center'>
<MessageSquare size={20} className='text-white' />
</div>
<div>
<Typography.Title heading={5} className='!text-white mb-0'>
{t('AI 对话')}
</Typography.Title>
<Typography.Text className='!text-white/80 text-sm hidden sm:inline'>
{inputs.model || t('选择模型开始对话')}
</Typography.Text>
</div>
</div>
<div className='flex items-center gap-2'>
<Button
icon={
showDebugPanel ? (
<ActionEyeOff size={14} />
) : (
<ActionEye size={14} />
)
}
onClick={onToggleDebugPanel}
theme='borderless'
type='primary'
size='small'
className='!rounded-lg !text-white/80 hover:!text-white hover:!bg-white/10'
>
{showDebugPanel ? t('隐藏调试') : t('显示调试')}
</Button>
</div>
</div>
</div>
)}
{/* 聊天内容区域 */}
<div
className={`flex-1 min-h-0 overflow-hidden ${styleState.isExperienceMode ? 'flex justify-center relative' : ''}`}
>
{/* 体验模式背景:页面级背景(TextModel.jsx 的渐变 + 光球)已铺满整屏,
这里不再叠不透明的 from-slate-50/90 via-white/80 主背景,否则会把
页面蓝色渐变整体盖住、显得发白。仅保留极轻的装饰网格/点纹增加质感。
聊天可读性由内层 max-w-4xl 容器的 bg-white/60 毛玻璃单独保证。 */}
{styleState.isExperienceMode && (
<div className='absolute inset-0 pointer-events-none'>
{/* 装饰性网格 */}
<div
className='absolute inset-0 opacity-[0.015] dark:opacity-[0.03]'
style={{
backgroundImage: `
radial-gradient(circle at 25% 25%, color-mix(in srgb, var(--brand-gradient-via) 40%, transparent) 0%, transparent 50%),
radial-gradient(circle at 75% 75%, color-mix(in srgb, var(--accent-blue) 40%, transparent) 0%, transparent 50%)
`,
}}
/>
{/* 细腻的点状纹理 */}
<div
className='absolute inset-0 opacity-[0.02] dark:opacity-[0.04]'
style={{
backgroundImage:
'radial-gradient(circle, color-mix(in srgb, var(--brand-gradient-via) 80%, transparent) 1px, transparent 1px)',
backgroundSize: '24px 24px',
}}
/>
</div>
)}
{styleState.isExperienceMode && message.length === 0 && (
<div
className={`absolute inset-0 flex flex-col items-center justify-center z-20 overflow-hidden px-4 ${hasActiveSession ? 'pointer-events-none' : 'pointer-events-auto'}`}
>
{/* 动态背景 - 欢迎页面专用 */}
<div className='absolute inset-0'>
{/* 渐变光晕 */}
<div className='absolute top-[10%] left-[15%] w-[300px] h-[300px] rounded-full bg-gradient-to-br from-blue-400/30 to-cyan-400/30 blur-[80px] animate-pulse' />
<div
className='absolute bottom-[15%] right-[10%] w-[350px] h-[350px] rounded-full bg-gradient-to-br from-brand-from/30 to-brand-to/30 blur-[80px] animate-pulse'
style={{ animationDelay: '1s' }}
/>
<div
className='absolute top-[50%] left-[50%] -translate-x-1/2 -translate-y-1/2 w-[200px] h-[200px] rounded-full bg-gradient-to-br from-pink-400/20 to-rose-400/20 blur-[60px] animate-pulse'
style={{ animationDelay: '2s' }}
/>
</div>
<div className='relative z-10 text-center max-w-3xl w-full'>
{/* Logo 和标题 */}
<div className='mb-10'>
<div className='relative inline-block mb-6'>
{/* 外层光环 */}
<div className='absolute -inset-4 bg-gradient-to-r from-blue-500/20 via-brand-via/20 to-brand-to/20 rounded-[32px] blur-xl animate-pulse' />
{/* 主图标 */}
<div className='relative w-24 h-24 rounded-3xl bg-gradient-to-br from-blue-500 via-brand-via to-brand-to flex items-center justify-center shadow-2xl shadow-brand/40'>
<MessageCircle size={48} className='text-white' />
{/* 闪烁装饰 */}
<Sparkles
size={20}
className='absolute -top-2 -right-2 text-amber-400 animate-bounce'
/>
<Zap
size={16}
className='absolute -bottom-1 -left-1 text-cyan-400 animate-pulse'
/>
</div>
</div>
<h1 className='text-3xl md:text-4xl font-bold mb-3'>
<span className='bg-clip-text text-transparent bg-gradient-to-r from-blue-600 via-brand-via to-brand-to dark:from-blue-400 dark:via-brand-via dark:to-brand-to'>
{t('AI 智能对话')}
</span>
</h1>
<p className='text-gray-500 dark:text-gray-400 text-base md:text-lg max-w-md mx-auto'>
{t('探索无限可能,开启智能对话之旅')}
</p>
</div>
{/* 功能卡片 */}
<div className='grid grid-cols-2 md:grid-cols-4 gap-3 mb-8'>
{featureCards.map((card, index) => (
<div
key={index}
className='group p-4 rounded-2xl bg-white/70 dark:bg-neutral-900/70 backdrop-blur-sm border border-gray-100 dark:border-neutral-800 shadow-sm hover:shadow-xl transition-all duration-300 hover:-translate-y-1 pointer-events-auto cursor-default'
>
<div
className={`w-10 h-10 rounded-xl ${card.bgLight} dark:bg-opacity-20 ${card.textColor} flex items-center justify-center mb-3 group-hover:scale-110 transition-transform`}
>
<card.icon size={20} />
</div>
<h3 className='font-semibold text-gray-800 dark:text-gray-200 text-sm mb-1'>
{card.title}
</h3>
<p className='text-xs text-gray-500 dark:text-gray-400'>
{card.description}
</p>
</div>
))}
</div>
{hasActiveSession ? (
<div className='space-y-2'>
<p className='text-xs text-gray-400 dark:text-gray-500 mb-3'>
{t('试试这些问题')}
</p>
<div className='flex flex-wrap justify-center gap-2'>
{quickPrompts.map((prompt, index) => (
<button
key={index}
onClick={() => onMessageSend(prompt)}
className='pointer-events-auto cursor-pointer px-4 py-2 rounded-full text-xs font-medium bg-white/80 dark:bg-neutral-800/80 backdrop-blur-sm border border-gray-200 dark:border-neutral-700 text-gray-600 dark:text-gray-300 hover:bg-brand/5 dark:hover:bg-brand-deep/30 hover:border-brand-from dark:hover:border-brand-deep hover:text-brand-deep dark:hover:text-brand-from transition-all duration-200 hover:shadow-md active:scale-95'
>
{prompt}
</button>
))}
</div>
</div>
) : (
<div className='space-y-4'>
<p className='text-sm text-gray-500 dark:text-gray-400'>
{t('先创建一个会话,再开始你的提问。')}
</p>
<div className='flex flex-wrap justify-center gap-3 pointer-events-auto'>
<Button
theme='solid'
type='primary'
loading={sessionCreating}
onClick={() => onCreateSession?.()}
className='!rounded-full'
>
{t('新建对话')}
</Button>
{styleState.isMobile && (
<Button
theme='light'
type='tertiary'
onClick={() => onOpenSessionDrawer?.()}
className='!rounded-full'
>
{t('查看会话列表')}
</Button>
)}
</div>
</div>
)}
</div>
</div>
)}
<div
className={`h-full w-full relative ${styleState.isExperienceMode ? 'max-w-4xl px-4 z-10' : ''}`}
>
{/* 体验模式下的聊天容器毛玻璃效果 */}
{styleState.isExperienceMode && message.length > 0 && (
<div
className='absolute inset-x-0 top-2 bottom-2 mx-4 rounded-3xl bg-white/60 dark:bg-neutral-900/60 backdrop-blur-xl border border-white/50 dark:border-neutral-700/50 shadow-xl shadow-brand/5 pointer-events-none'
style={{ zIndex: 0 }}
/>
)}
{/* 图片生成模式设置栏 */}
{styleState.isExperienceMode && imageGenMode && (
<div className='flex flex-wrap items-center gap-x-3 gap-y-2 px-4 sm:px-6 py-2 bg-brand/80 dark:bg-brand-deep/20 backdrop-blur-sm border-b border-brand/50 dark:border-brand-deep/50 rounded-t-2xl relative z-10'>
<div className='flex items-center gap-1.5'>
<Paintbrush size={14} className='text-brand' />
<span className='text-xs font-medium text-brand-deep dark:text-brand-from'>
{t('图片生成模式')}
</span>
</div>
<Select
placeholder={t('模型')}
value={imageGenModel}
onChange={(val) => onImageGenModelChange(val)}
optionList={ecImageGenModels}
size='small'
style={{ width: 160 }}
/>
<Select
placeholder={t('尺寸')}
value={imageGenSize}
onChange={(val) => onImageGenSizeChange(val)}
optionList={imageSizeOptions}
size='small'
style={{ width: 130 }}
/>
</div>
)}
{/* 摘要 chip:摘要激活时持续显示,让用户能感知「历史已被压缩」并提供管理入口。
与下面的长对话提示互斥(hasSummary 时已抑制 showLongConversationHint)。 */}
{showSummaryChip && (
<div className='absolute top-2 left-1/2 -translate-x-1/2 z-20 max-w-[92%]'>
<Popover
trigger='click'
position='bottom'
content={
<div className='p-3 max-w-[520px] max-h-[60vh] overflow-y-auto'>
<div className='flex items-center justify-between mb-2 gap-2'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-200'>
{t('对话历史摘要')}
</span>
<div className='flex items-center gap-1'>
<Button
size='small'
theme='borderless'
disabled={summaryActionLoading !== null}
loading={summaryActionLoading === 'regen'}
onClick={async () => {
if (!onRegenerateSummary) return;
setSummaryActionLoading('regen');
try {
await onRegenerateSummary();
} finally {
setSummaryActionLoading(null);
}
}}
>
{t('重新生成')}
</Button>
<Button
size='small'
theme='borderless'
type='danger'
disabled={summaryActionLoading !== null}
loading={summaryActionLoading === 'clear'}
onClick={async () => {
if (!onClearSummary) return;
setSummaryActionLoading('clear');
try {
await onClearSummary();
} finally {
setSummaryActionLoading(null);
}
}}
>
{t('清空摘要')}
</Button>
</div>
</div>
<div className='text-xs text-gray-500 dark:text-gray-400 mb-2'>
{t(
'发送给模型的请求会用本段摘要代替较早的历史,节省 token 消耗。重新生成或清空会产生相应的计费变化。',
)}
</div>
<div className='text-sm text-gray-700 dark:text-gray-200 whitespace-pre-wrap break-words leading-relaxed'>
{conversationSummary}
</div>
</div>
}
>
<button
type='button'
className='flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-brand/95 dark:bg-brand-deep/30 backdrop-blur-sm border border-brand/80 dark:border-brand-deep/60 shadow-md text-xs text-brand-deep dark:text-brand-from hover:bg-brand/95 dark:hover:bg-brand-deep/50 transition'
aria-label={t('查看对话历史摘要')}
>
<span>📝</span>
<span>{t('历史已压缩,点击查看摘要')}</span>
</button>
</Popover>
</div>
)}
{/* Feature C:长会话「新建对话」引导 —— 悬浮在聊天区顶部,可关闭。
与摘要 chip 互斥:摘要激活后这条不再出现(产品语义上摘要已经替你压缩了上下文)。 */}
{showLongConversationHint && (
<div className='absolute top-2 left-1/2 -translate-x-1/2 z-20 flex items-center gap-2 px-3 py-1.5 rounded-full bg-amber-50/95 dark:bg-amber-900/40 backdrop-blur-sm border border-amber-200/80 dark:border-amber-700/60 shadow-md text-xs max-w-[92%]'>
<span className='text-amber-700 dark:text-amber-300 truncate'>
{ecSummaryThreshold > 0
? t(
'当前对话已 {{rounds}} 轮,即将触发自动摘要;如希望话题更聚焦也可新建对话',
{ rounds: conversationRounds },
)
: t(
'当前对话已 {{rounds}} 轮,较长的上下文会增加消耗,建议新建对话',
{ rounds: conversationRounds },
)}
</span>
<Button
size='small'
theme='solid'
type='warning'
className='!rounded-full flex-shrink-0'
loading={sessionCreating}
onClick={() => onCreateSession?.()}
>
{t('新建对话')}
</Button>
<button
type='button'
onClick={() => setLongConvHintDismissed(true)}
className='app-action-button flex-shrink-0 text-amber-500 hover:text-amber-700 dark:hover:text-amber-300'
aria-label={t('关闭提示')}
>
<ActionX size={14} />
</button>
</div>
)}
<Chat
ref={chatRef}
chatBoxRenderConfig={{
renderChatBoxContent: renderCustomChatContent,
renderChatBoxAction: renderChatBoxAction,
renderChatBoxTitle: () => null,
}}
renderInputArea={renderInputArea}
roleConfig={roleInfo}
style={{
height: '100%',
width: '100%',
maxWidth: '100%',
position: 'relative',
zIndex: 1,
}}
chats={message}
onMessageSend={onMessageSend}
onMessageCopy={onMessageCopy}
onMessageReset={onMessageReset}
onMessageDelete={onMessageDelete}
showClearContext
showStopGenerate
onStopGenerator={onStopGenerator}
onClear={onClearMessages}
className={`h-full ${styleState.isExperienceMode ? 'experience-chat-container' : ''}`}
placeholder={chatPlaceholder}
/>
</div>
</div>
</Card>
{/* 回到最新悬浮按钮 —— 用户上滚看历史时显示,避免错过流式输出
放在最外层 wrapper,定位用 absolute;底部偏移留出 CustomInputRender 的高度 */}
{showScrollToBottom && (
<button
type='button'
onClick={() => scrollToBottom()}
style={{ bottom: 'calc(var(--composer-height, 90px) + 14px)' }}
className='app-action-button \n absolute z-30 left-1/2 -translate-x-1/2\n flex items-center gap-1.5 px-3 py-1.5 rounded-full\n bg-white/95 dark:bg-neutral-800/95 backdrop-blur-sm\n border border-gray-200/80 dark:border-neutral-700/80\n text-xs font-medium text-gray-700 dark:text-gray-200\n shadow-md hover:shadow-lg hover:scale-105 active:scale-95\n transition-all duration-200\n '
title={t('回到最新消息')}
>
<ActionArrowDown size={13} className='text-brand' />
{t('回到最新')}
</button>
)}
{/* 多选导出底部悬浮条:定位仿"回到最新"pill;手机端拉满宽显示两行。
底部偏移不能写死 —— 手机端输入区是「输入行 + 工具行」双行结构,比桌面高一截,
写死的 bottom-32/sm:bottom-28 会落在工具按钮上并挡住点击。
改读 CustomInputRender 实测发布的 --composer-height。 */}
{selectionMode && (
<div
style={{ bottom: 'calc(var(--composer-height, 90px) + 14px)' }}
className={`absolute z-30 ${
styleState.isMobile ? 'left-2 right-2' : 'left-1/2 -translate-x-1/2'
}`}
>
<MessageSelectionBar
selectedCount={selectedCount}
selectableCount={selectableCount}
isAllSelected={isAllSelected}
onSelectAll={onSelectAll}
onClearSelection={onClearSelection}
onExport={onExportSelected}
onCancel={onExitSelectionMode}
isMobile={styleState.isMobile}
/>
</div>
)}
</div>
);
};
export default ChatArea;