| | |
| | | 'use client'; |
| | | |
| | | import { useState, useEffect, useRef, useCallback } from 'react'; |
| | | import { useState, useEffect, useRef, useCallback, useContext, createContext } from 'react'; |
| | | import Link from 'next/link'; |
| | | import { useRouter } from 'next/navigation'; |
| | | import Image from 'next/image'; |
| | |
| | | import remarkGfm from 'remark-gfm'; |
| | | import rehypeRaw from 'rehype-raw'; |
| | | import rehypeSanitize from 'rehype-sanitize'; |
| | | import dynamic from 'next/dynamic'; |
| | | import { ReactNode } from 'react'; |
| | | |
| | | // 创建一个消息完成状态的Context |
| | | const MessageCompletionContext = createContext<boolean>(true); |
| | | |
| | | // 动态导入 ECharts,确保它只在客户端渲染 |
| | | const ReactECharts = dynamic(() => import('echarts-for-react'), { ssr: false }); |
| | | |
| | | interface Message { |
| | | role: 'user' | 'assistant'; |
| | |
| | | // 默认用户头像 |
| | | const DEFAULT_USER_AVATAR = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23999999'%3E%3Cpath d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 3c1.66 0 3 1.34 3 3s-1.34 3-3 3-3-1.34-3-3 1.34-3 3-3zm0 14.2c-2.5 0-4.71-1.28-6-3.22.03-1.99 4-3.08 6-3.08 1.99 0 5.97 1.09 6 3.08-1.29 1.94-3.5 3.22-6 3.22z'/%3E%3C/svg%3E"; |
| | | const AI_AVATAR = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%234F46E5'%3E%3Cpath d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 1c4.96 0 9 4.04 9 9s-4.04 9-9 9-9-4.04-9-9 4.04-9 9-9zm0 3.5c-2.48 0-4.5 2.02-4.5 4.5h1.5c0-1.66 1.34-3 3-3s3 1.34 3 3h1.5c0-2.48-2.02-4.5-4.5-4.5zM8 13h2v2H8v-2zm6 0h2v2h-2v-2z'/%3E%3C/svg%3E"; |
| | | |
| | | // 添加用于解析和渲染 ECharts 的组件 |
| | | function EchartsRenderer({ code }: { code: string }) { |
| | | const [error, setError] = useState<string | null>(null); |
| | | const [isLoading, setIsLoading] = useState(true); |
| | | const [isModalOpen, setIsModalOpen] = useState(false); |
| | | const chartContainerRef = useRef<HTMLDivElement>(null); |
| | | const modalChartRef = useRef<HTMLDivElement>(null); |
| | | const hasRenderedRef = useRef<boolean>(false); |
| | | const chartInstanceRef = useRef<any>(null); |
| | | |
| | | // 初始化DOM容器 - 提前设置固定高度 |
| | | useEffect(() => { |
| | | // 确保容器准备好了并且有确定的高度 |
| | | if (chartContainerRef.current) { |
| | | // 设置一个固定的高度,避免渲染后变化 |
| | | const container = chartContainerRef.current; |
| | | container.style.width = '100%'; |
| | | container.style.height = '400px'; // 固定高度400px |
| | | container.style.minHeight = '400px'; // 防止高度变小 |
| | | |
| | | // 添加临时内容,确保DOM渲染完成 |
| | | container.innerHTML = '<div style="width:100%;height:100%;display:flex;align-items:center;justify-content:center;background:#f5f7f9;"><span>正在准备图表...</span></div>'; |
| | | } |
| | | }, []); |
| | | |
| | | // 一次性加载图表,不进行动态更新 |
| | | useEffect(() => { |
| | | // 如果已经渲染过,跳过 |
| | | if (hasRenderedRef.current) { |
| | | return; |
| | | } |
| | | |
| | | setIsLoading(true); |
| | | |
| | | // 确保DOM元素已经完全挂载和渲染 |
| | | const timer = setTimeout(() => { |
| | | // 确保DOM元素仍然存在 |
| | | if (!chartContainerRef.current) { |
| | | console.error('图表容器不存在,跳过初始化'); |
| | | setIsLoading(false); |
| | | return; |
| | | } |
| | | |
| | | // 清除临时内容 |
| | | chartContainerRef.current.innerHTML = ''; |
| | | |
| | | // 标记为已渲染 |
| | | hasRenderedRef.current = true; |
| | | |
| | | // 初始化图表 |
| | | const initializeChart = async () => { |
| | | try { |
| | | const echarts = await import('echarts'); |
| | | |
| | | // 初始化图表 |
| | | const chartInstance = echarts.init(chartContainerRef.current); |
| | | chartInstanceRef.current = chartInstance; |
| | | |
| | | // 尝试解析和设置图表选项 |
| | | try { |
| | | const safeCode = code.replace(/window\.option/g, 'option'); |
| | | const safeFunc = new Function(` |
| | | "use strict"; |
| | | let option; |
| | | try { |
| | | ${safeCode} |
| | | return option; |
| | | } catch (e) { |
| | | console.error("图表代码执行错误:", e); |
| | | return null; |
| | | } |
| | | `); |
| | | |
| | | const chartOption = safeFunc(); |
| | | |
| | | if (chartOption) { |
| | | // 禁用动画,避免渲染时的布局变化 |
| | | chartOption.animation = false; |
| | | |
| | | // 修改tooltip配置,确保显示在适当位置 |
| | | if (chartOption.tooltip) { |
| | | chartOption.tooltip = { |
| | | ...chartOption.tooltip, |
| | | confine: false, // 不限制在图表区域内 |
| | | extraCssText: 'z-index:9999; pointer-events:auto; margin-top:0;' |
| | | }; |
| | | } |
| | | |
| | | chartInstance.setOption(chartOption); |
| | | setError(null); |
| | | } else { |
| | | throw new Error("无法获取图表配置"); |
| | | } |
| | | } catch (e) { |
| | | console.error('图表代码执行错误:', e); |
| | | setError('图表配置错误'); |
| | | |
| | | // 设置一个默认图表以显示错误 |
| | | chartInstance.setOption({ |
| | | title: { text: '图表配置错误' }, |
| | | xAxis: { type: 'category', data: ['错误'] }, |
| | | yAxis: { type: 'value' }, |
| | | series: [{ data: [0], type: 'bar' }] |
| | | }); |
| | | } |
| | | |
| | | // 添加窗口大小变化监听器 |
| | | const handleResize = () => chartInstance.resize(); |
| | | window.addEventListener('resize', handleResize); |
| | | |
| | | // 清理函数 |
| | | return () => { |
| | | window.removeEventListener('resize', handleResize); |
| | | chartInstance.dispose(); |
| | | }; |
| | | } catch (e) { |
| | | console.error('ECharts加载失败:', e); |
| | | setError('图表库加载失败'); |
| | | } finally { |
| | | setIsLoading(false); |
| | | } |
| | | }; |
| | | |
| | | initializeChart(); |
| | | }, 500); // 增加延迟,确保DOM完全渲染 |
| | | |
| | | return () => clearTimeout(timer); |
| | | }, [code]); |
| | | |
| | | // 当全屏状态变化时重新调整图表大小 |
| | | useEffect(() => { |
| | | if (chartInstanceRef.current) { |
| | | setTimeout(() => { |
| | | chartInstanceRef.current.resize(); |
| | | }, 300); // 给DOM一些时间来更新 |
| | | } |
| | | }, [isModalOpen]); |
| | | |
| | | // 处理全屏切换 |
| | | const toggleModal = useCallback(() => { |
| | | setIsModalOpen(prev => !prev); |
| | | }, []); |
| | | |
| | | // 处理模态窗口的图表 |
| | | useEffect(() => { |
| | | if (!isModalOpen || !modalChartRef.current) return; |
| | | |
| | | const initModalChart = async () => { |
| | | try { |
| | | const echarts = await import('echarts'); |
| | | |
| | | // 初始化模态窗口中的图表 |
| | | const modalChartInstance = echarts.init(modalChartRef.current); |
| | | |
| | | // 如果主图表已经初始化,复用其配置 |
| | | if (chartInstanceRef.current) { |
| | | const option = chartInstanceRef.current.getOption(); |
| | | |
| | | // 直接使用主图表的完整配置,不做额外修改 |
| | | modalChartInstance.setOption(option); |
| | | } else { |
| | | // 如果主图表没有初始化,尝试从代码初始化 |
| | | try { |
| | | const safeCode = code.replace(/window\.option/g, 'option'); |
| | | const safeFunc = new Function(` |
| | | "use strict"; |
| | | let option; |
| | | try { |
| | | ${safeCode} |
| | | return option; |
| | | } catch (e) { |
| | | console.error("模态图表代码执行错误:", e); |
| | | return null; |
| | | } |
| | | `); |
| | | |
| | | const chartOption = safeFunc(); |
| | | |
| | | if (chartOption) { |
| | | // 与主图表使用完全相同的配置 |
| | | modalChartInstance.setOption(chartOption); |
| | | } else { |
| | | throw new Error("无法获取模态图表配置"); |
| | | } |
| | | } catch (e) { |
| | | console.error('模态图表代码执行错误:', e); |
| | | |
| | | // 设置一个默认图表 |
| | | modalChartInstance.setOption({ |
| | | title: { text: '图表配置错误' }, |
| | | xAxis: { type: 'category', data: ['错误'] }, |
| | | yAxis: { type: 'value' }, |
| | | series: [{ data: [0], type: 'bar' }] |
| | | }); |
| | | } |
| | | } |
| | | |
| | | // 添加窗口大小变化监听器 |
| | | const handleResize = () => modalChartInstance.resize(); |
| | | window.addEventListener('resize', handleResize); |
| | | |
| | | // 返回清理函数 |
| | | return () => { |
| | | window.removeEventListener('resize', handleResize); |
| | | modalChartInstance.dispose(); |
| | | }; |
| | | } catch (e) { |
| | | console.error('模态图表初始化失败:', e); |
| | | } |
| | | }; |
| | | |
| | | // 延迟一点执行,确保DOM已更新完成 |
| | | const timer = setTimeout(initModalChart, 100); |
| | | return () => clearTimeout(timer); |
| | | }, [isModalOpen, code]); |
| | | |
| | | // 简化的渲染逻辑,确保容器有明确的尺寸 |
| | | return ( |
| | | <div className="relative my-4"> |
| | | {isLoading && ( |
| | | <div className="absolute inset-0 flex items-center justify-center bg-white/70 z-10"> |
| | | <div className="flex flex-col items-center"> |
| | | <div className="animate-spin rounded-full h-10 w-10 border-t-2 border-b-2 border-blue-500 mb-3"></div> |
| | | <p className="text-gray-500">图表加载中...</p> |
| | | </div> |
| | | </div> |
| | | )} |
| | | |
| | | {error && ( |
| | | <div className="absolute top-0 left-0 right-0 bg-red-50 border border-red-200 text-red-600 px-3 py-2 rounded-md text-xs z-10"> |
| | | {error} |
| | | </div> |
| | | )} |
| | | |
| | | {/* 图表容器 - 固定高度 */} |
| | | <div |
| | | ref={chartContainerRef} |
| | | className="w-full bg-white border border-gray-200 rounded-lg overflow-hidden chart-container" |
| | | style={{ |
| | | height: '400px', |
| | | minHeight: '400px', |
| | | visibility: 'visible', |
| | | position: 'relative' |
| | | }} |
| | | data-echarts-container="true" |
| | | /> |
| | | |
| | | {/* 打开弹窗按钮 */} |
| | | <button |
| | | onClick={() => setIsModalOpen(true)} |
| | | className="absolute top-2 right-2 bg-white/90 hover:bg-white p-2 rounded-full shadow-md z-20 transition-all duration-300 hover:scale-110 cursor-pointer" |
| | | title="在弹窗中查看" |
| | | > |
| | | <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| | | <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 3h6v6m0-6L14 9M9 21H3v-6m0 6l7-7" /> |
| | | </svg> |
| | | </button> |
| | | |
| | | {/* 弹出式对话框 */} |
| | | {isModalOpen && ( |
| | | <div className="fixed inset-0 z-[100] flex items-center justify-center bg-black/50 p-4"> |
| | | <div className="bg-white rounded-lg w-[90vw] max-w-6xl h-[80vh] relative flex flex-col"> |
| | | {/* 对话框头部 */} |
| | | <div className="flex justify-between items-center p-4 border-b"> |
| | | <h3 className="text-xl font-semibold text-gray-800">数据可视化图表</h3> |
| | | <button |
| | | onClick={() => setIsModalOpen(false)} |
| | | className="p-1 rounded-full hover:bg-gray-100 cursor-pointer" |
| | | aria-label="关闭" |
| | | > |
| | | <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| | | <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /> |
| | | </svg> |
| | | </button> |
| | | </div> |
| | | |
| | | {/* 对话框主体 - 图表 */} |
| | | <div className="flex-1 overflow-hidden p-4"> |
| | | <div |
| | | ref={modalChartRef} |
| | | className="w-full h-full" |
| | | data-echarts-modal |
| | | ></div> |
| | | </div> |
| | | </div> |
| | | </div> |
| | | )} |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | // 修改代码块渲染组件,添加更好的类型检测 |
| | | function CodeBlockRenderer({ language, value }: { language: string; value: string }) { |
| | | // 判断是否是JavaScript代码,检测是否包含图表相关特征 |
| | | const isEchartsCode = useCallback(() => { |
| | | if (language !== 'javascript') return false; |
| | | |
| | | // 检查是否包含ECharts特有的配置项 |
| | | return value.includes('option') && |
| | | (value.includes('series') || |
| | | value.includes('chart') || |
| | | value.includes('echarts') || |
| | | value.includes('xAxis') || |
| | | value.includes('yAxis')); |
| | | }, [language, value]); |
| | | |
| | | // 检查消息是否完整 - 通过父组件传递的isMessageComplete状态 |
| | | const isComplete = useContext(MessageCompletionContext); |
| | | |
| | | // 在消息未完成时显示加载动画 - 固定高度匹配图表 |
| | | if (language === 'javascript' && !isComplete) { |
| | | return ( |
| | | <div className="w-full bg-gray-50 rounded-md my-4 p-6 text-center" style={{height: '400px'}}> |
| | | <div className="flex flex-col items-center justify-center h-full"> |
| | | <div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-gray-400 mb-4"></div> |
| | | <p className="text-gray-500">加载中...</p> |
| | | <p className="text-xs text-gray-400 mt-2">等待消息完成后将渲染图表</p> |
| | | </div> |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | // 消息完成后,如果是图表代码则渲染为图表 - 在外部包装一层固定高度容器 |
| | | if (isEchartsCode() && isComplete) { |
| | | return ( |
| | | <div style={{height: '400px', minHeight: '400px', position: 'relative'}}> |
| | | <EchartsRenderer code={value} /> |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | // 普通JavaScript代码或其他语言的代码块,直接显示代码 |
| | | return ( |
| | | <div className="bg-gray-800 rounded-md my-2 overflow-hidden"> |
| | | <div className="flex items-center justify-between px-4 py-2 border-b border-gray-700"> |
| | | <span className="text-xs text-gray-400">{language}</span> |
| | | </div> |
| | | <pre className="p-4 text-gray-300 overflow-x-auto"> |
| | | <code>{value}</code> |
| | | </pre> |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | // 创建独立的输入组件,避免状态共享导致的重渲染 |
| | | interface ChatInputProps { |
| | | onSendMessage: () => void; |
| | | isStreaming: boolean; |
| | | isMessageComplete: boolean; |
| | | } |
| | | |
| | | function ChatInput({ onSendMessage, isStreaming, isMessageComplete }: ChatInputProps) { |
| | | // 内部状态,与外部完全隔离 |
| | | const [inputText, setInputText] = useState(''); |
| | | const inputRef = useRef<HTMLTextAreaElement>(null); |
| | | |
| | | // 处理输入变化 |
| | | const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| | | setInputText(e.target.value); |
| | | }; |
| | | |
| | | // 处理按键事件 |
| | | const handleKeyDown = (e: React.KeyboardEvent) => { |
| | | if (e.key === 'Enter' && !e.shiftKey) { |
| | | e.preventDefault(); |
| | | handleSend(); |
| | | } |
| | | }; |
| | | |
| | | // 处理发送 |
| | | const handleSend = () => { |
| | | if (isStreaming || !inputText.trim() || !isMessageComplete) return; |
| | | |
| | | // 通知父组件发送消息前更新消息内容 |
| | | (window as any).messageToSend = inputText.trim(); |
| | | |
| | | // 清空输入 |
| | | setInputText(''); |
| | | |
| | | // 调用父组件的发送方法 |
| | | onSendMessage(); |
| | | }; |
| | | |
| | | return ( |
| | | <div className="fixed bottom-0 left-0 right-0 bg-gradient-to-t from-white via-white to-white/95 pt-4 pb-6" style={{ zIndex: 50 }}> |
| | | <div className="max-w-4xl mx-auto px-6"> |
| | | <div className="relative"> |
| | | <div className="absolute -top-6 left-1/2 -translate-x-1/2 w-48 h-[1px] bg-gradient-to-r from-transparent via-gray-200 to-transparent"></div> |
| | | |
| | | <div className="flex gap-4 items-start"> |
| | | <div className="flex-1 relative"> |
| | | <textarea |
| | | ref={inputRef} |
| | | value={inputText} |
| | | onChange={handleInputChange} |
| | | onKeyDown={handleKeyDown} |
| | | placeholder="输入消息..." |
| | | disabled={isStreaming} |
| | | className="w-full resize-none rounded-xl border-0 bg-gray-50 px-4 py-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-blue-100/50 focus:bg-white min-h-[48px] max-h-32 shadow-inner transition-all duration-300 ease-in-out hover:bg-gray-100/70 disabled:opacity-50 disabled:cursor-not-allowed" |
| | | style={{ height: '48px' }} |
| | | /> |
| | | <div className="absolute right-4 bottom-2 text-xs text-gray-400 bg-gray-50 px-2"> |
| | | 按Enter发送,Shift+Enter换行 |
| | | </div> |
| | | </div> |
| | | <button |
| | | onClick={handleSend} |
| | | disabled={isStreaming || !inputText.trim() || !isMessageComplete} |
| | | className="h-12 px-5 bg-gradient-to-r from-blue-500 to-blue-600 hover:from-blue-600 hover:to-blue-700 text-white font-medium rounded-xl transition-all duration-300 flex items-center gap-2 shadow-lg shadow-blue-500/20 hover:shadow-blue-500/30 hover:scale-[1.02] active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed disabled:transform-none disabled:shadow-none cursor-pointer" |
| | | > |
| | | <span>{isStreaming ? '回复中...' : (isMessageComplete ? '发送' : '处理中...')}</span> |
| | | <svg xmlns="http://www.w3.org/2000/svg" className={`h-4 w-4 transform rotate-45 ${isStreaming ? 'animate-pulse' : ''}`} viewBox="0 0 20 20" fill="currentColor"> |
| | | <path d="M10.894 2.553a1 1 0 00-1.788 0l-7 14a1 1 0 001.169 1.409l5-1.429A1 1 0 009 15.571V11a1 1 0 112 0v4.571a1 1 0 00.725.962l5 1.428a1 1 0 001.17-1.408l-7-14z" /> |
| | | </svg> |
| | | </button> |
| | | </div> |
| | | </div> |
| | | </div> |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | export default function Page() { |
| | | const router = useRouter(); |
| | |
| | | |
| | | // 在组件顶部添加一个引用,用于跟踪组件是否已卸载 |
| | | const isMountedRef = useRef(true); |
| | | |
| | | // 分离消息输入状态,避免触发不必要的重渲染 |
| | | const messageInputRef = useRef<HTMLTextAreaElement>(null); |
| | | const [localMessage, setLocalMessage] = useState(''); |
| | | |
| | | const handleMessageChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| | | // 只更新本地状态,不触发全局重渲染 |
| | | setLocalMessage(e.target.value); |
| | | }; |
| | | |
| | | // 仅在发送时更新全局消息状态 |
| | | const handleKeyPress = (e: React.KeyboardEvent) => { |
| | | if (e.key === 'Enter' && !e.shiftKey) { |
| | | e.preventDefault(); |
| | | // 更新全局状态并发送 |
| | | setMessage(localMessage); |
| | | setTimeout(() => handleSendMessage(), 10); |
| | | } |
| | | }; |
| | | |
| | | // 点击发送按钮时 |
| | | const handleSendButtonClick = () => { |
| | | // 检查消息是否为空 |
| | | if (!localMessage.trim() || !isMessageComplete) return; |
| | | |
| | | // 更新全局状态并发送 |
| | | setMessage(localMessage); |
| | | setTimeout(() => handleSendMessage(), 10); |
| | | }; |
| | | |
| | | // 同步消息状态到本地状态 |
| | | useEffect(() => { |
| | | setLocalMessage(message); |
| | | }, [message]); |
| | | |
| | | useEffect(() => { |
| | | // 获取存储的API Key |
| | |
| | | }; |
| | | |
| | | const handleSendMessage = async () => { |
| | | if (!message.trim() || !isMessageComplete) return; |
| | | // 从window对象获取消息内容 |
| | | const inputMessage = (window as any).messageToSend || ''; |
| | | if (!inputMessage.trim() || !isMessageComplete) return; |
| | | if (!apiKey) { |
| | | showErrorMessage('请先设置API Key'); |
| | | return; |
| | |
| | | // 创建新消息 |
| | | const userMessage: Message = { |
| | | role: 'user', |
| | | content: message.trim(), |
| | | content: inputMessage.trim(), |
| | | timestamp: Date.now(), |
| | | id: 'user-' + Date.now().toString() |
| | | }; |
| | |
| | | |
| | | setCurrentMessageId(assistantMessage.id); |
| | | setMessage(''); |
| | | |
| | | // 清除输入内容 |
| | | (window as any).messageToSend = ''; |
| | | |
| | | let controller: AbortController | null = new AbortController(); |
| | | |
| | |
| | | }); |
| | | }, []); |
| | | |
| | | // 更新发送按钮的禁用状态 |
| | | const isSendDisabled = isStreaming || !message.trim() || !isMessageComplete; |
| | | |
| | | const handleKeyPress = (e: React.KeyboardEvent) => { |
| | | if (e.key === 'Enter' && !e.shiftKey) { |
| | | e.preventDefault(); |
| | | handleSendMessage(); |
| | | } |
| | | }; |
| | | |
| | | useEffect(() => { |
| | | // 页面加载后延迟显示消息列表,避免闪烁 |
| | | const timer = setTimeout(() => { |
| | |
| | | .dot-animation span:nth-child(1) { animation: dotBounce 1.4s -0.32s infinite ease-in-out; } |
| | | .dot-animation span:nth-child(2) { animation: dotBounce 1.4s -0.16s infinite ease-in-out; } |
| | | .dot-animation span:nth-child(3) { animation: dotBounce 1.4s 0s infinite ease-in-out; } |
| | | |
| | | /* 优化图表容器样式,不再使用isolation和will-change */ |
| | | .echart-wrapper { |
| | | position: relative; |
| | | z-index: auto; |
| | | } |
| | | |
| | | /* 使用更温和的性能优化属性,避免层级错误 */ |
| | | canvas { |
| | | transition: none !important; |
| | | } |
| | | |
| | | /* 修复输入框遮挡问题 */ |
| | | .fixed.bottom-0 { |
| | | z-index: 10; |
| | | } |
| | | |
| | | /* 防止图表闪烁但不影响其他元素 */ |
| | | .echart-wrapper > div { |
| | | transform: translateZ(0); |
| | | will-change: transform; |
| | | transition: none !important; |
| | | } |
| | | |
| | | /* 修复echarts渲染问题 */ |
| | | [data-echarts-container] { |
| | | visibility: visible !important; |
| | | opacity: 1 !important; |
| | | width: 100% !important; |
| | | contain: none !important; |
| | | } |
| | | `; |
| | | document.head.appendChild(style); |
| | | |
| | |
| | | : 'bg-[#E8F4FF] rounded-xl' |
| | | } inline-block max-w-[85%] relative overflow-hidden`}> |
| | | |
| | | {/* 调试信息 - 移除这部分 */} |
| | | {/* |
| | | {process.env.NODE_ENV !== 'production' && ( |
| | | <div className="bg-gray-100 px-2 py-1 text-xs text-gray-500"> |
| | | 长度: {(msg.content || '').length} | 计数: {forceUpdateCounter} |
| | | </div> |
| | | )} |
| | | */} |
| | | |
| | | {msg.role === 'assistant' && ( |
| | | <>{(() => { |
| | | // 解析消息内容,提取思考部分 |
| | |
| | | |
| | | {/* 主要内容 */} |
| | | <div className="p-3"> |
| | | <div className="text-gray-800 leading-relaxed whitespace-pre-wrap"> |
| | | {mainContent || (msg.role === 'assistant' && !isMessageComplete ? '正在思考...' : '')} |
| | | <div className="text-gray-800 leading-relaxed"> |
| | | {mainContent ? ( |
| | | <MessageCompletionContext.Provider |
| | | value={msg.id !== currentMessageId || isMessageComplete} |
| | | > |
| | | <ReactMarkdown |
| | | remarkPlugins={[remarkGfm]} |
| | | rehypePlugins={[rehypeRaw, rehypeSanitize]} |
| | | components={{ |
| | | // @ts-ignore - ReactMarkdown 组件类型定义的兼容性问题 |
| | | code: ({ node, inline, className, children, ...props }) => { |
| | | const match = /language-(\w+)/.exec(className || ''); |
| | | const language = match ? match[1] : ''; |
| | | const value = String(children).replace(/\n$/, ''); |
| | | |
| | | if (!inline && match) { |
| | | return <CodeBlockRenderer language={language} value={value} />; |
| | | } |
| | | |
| | | return ( |
| | | <code className={className} {...props}> |
| | | {children} |
| | | </code> |
| | | ); |
| | | } |
| | | }} |
| | | > |
| | | {mainContent} |
| | | </ReactMarkdown> |
| | | </MessageCompletionContext.Provider> |
| | | ) : ( |
| | | msg.role === 'assistant' && !isMessageComplete ? '处理回复中...' : '' |
| | | )} |
| | | </div> |
| | | </div> |
| | | |
| | | {/* 加载指示器 */} |
| | | {msg.role === 'assistant' && !isMessageComplete && ( |
| | | {msg.role === 'assistant' && |
| | | !isMessageComplete && |
| | | msg.id === currentMessageId && ( // 只在当前处理的消息显示加载指示器 |
| | | <div className="absolute bottom-1 right-2"> |
| | | <div className="flex space-x-1"> |
| | | <div className="w-1.5 h-1.5 rounded-full bg-blue-400 animate-pulse delay-0"></div> |
| | |
| | | </div> |
| | | </div> |
| | | |
| | | {/* 输入区域 */} |
| | | <div className="fixed bottom-0 left-0 right-0 bg-gradient-to-t from-white via-white to-white/95 pt-4 pb-6"> |
| | | <div className="max-w-4xl mx-auto px-6"> |
| | | <div className="relative"> |
| | | <div className="absolute -top-6 left-1/2 -translate-x-1/2 w-48 h-[1px] bg-gradient-to-r from-transparent via-gray-200 to-transparent"></div> |
| | | |
| | | <div className="flex gap-4 items-start"> |
| | | <div className="flex-1 relative"> |
| | | <textarea |
| | | value={message} |
| | | onChange={(e) => setMessage(e.target.value)} |
| | | onKeyDown={handleKeyPress} |
| | | placeholder="输入消息..." |
| | | disabled={isStreaming} |
| | | className="w-full resize-none rounded-xl border-0 bg-gray-50 px-4 py-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-blue-100/50 focus:bg-white min-h-[48px] max-h-32 shadow-inner transition-all duration-300 ease-in-out hover:bg-gray-100/70 disabled:opacity-50 disabled:cursor-not-allowed" |
| | | style={{ height: '48px' }} |
| | | /> |
| | | <div className="absolute right-4 bottom-2 text-xs text-gray-400 bg-gray-50 px-2"> |
| | | 按Enter发送,Shift+Enter换行 |
| | | </div> |
| | | </div> |
| | | <button |
| | | onClick={handleSendMessage} |
| | | disabled={isSendDisabled} |
| | | className="h-12 px-5 bg-gradient-to-r from-blue-500 to-blue-600 hover:from-blue-600 hover:to-blue-700 text-white font-medium rounded-xl transition-all duration-300 flex items-center gap-2 shadow-lg shadow-blue-500/20 hover:shadow-blue-500/30 hover:scale-[1.02] active:scale-[0.98] disabled:opacity-50 disabled:cursor-not-allowed disabled:transform-none disabled:shadow-none" |
| | | > |
| | | <span>{isStreaming ? '回复中...' : (isMessageComplete ? '发送' : '处理中...')}</span> |
| | | <svg xmlns="http://www.w3.org/2000/svg" className={`h-4 w-4 transform rotate-45 ${isStreaming ? 'animate-pulse' : ''}`} viewBox="0 0 20 20" fill="currentColor"> |
| | | <path d="M10.894 2.553a1 1 0 00-1.788 0l-7 14a1 1 0 001.169 1.409l5-1.429A1 1 0 009 15.571V11a1 1 0 112 0v4.571a1 1 0 00.725.962l5 1.428a1 1 0 001.17-1.408l-7-14z" /> |
| | | </svg> |
| | | </button> |
| | | </div> |
| | | </div> |
| | | </div> |
| | | </div> |
| | | {/* 输入区域 - 抽取为独立组件 */} |
| | | <ChatInput |
| | | onSendMessage={handleSendMessage} |
| | | isStreaming={isStreaming} |
| | | isMessageComplete={isMessageComplete} |
| | | /> |
| | | </div> |
| | | </div> |
| | | ); |
| | | } |
| | | |
| | | } |